Moonad 3.0.0
See the version list below for details.
dotnet add package Moonad --version 3.0.0
NuGet\Install-Package Moonad -Version 3.0.0
<PackageReference Include="Moonad" Version="3.0.0" />
paket add Moonad --version 3.0.0
#r "nuget: Moonad, 3.0.0"
// Install Moonad as a Cake Addin #addin nuget:?package=Moonad&version=3.0.0 // Install Moonad as a Cake Tool #tool nuget:?package=Moonad&version=3.0.0
Moonad
A simple F#'s monads port for C#.
This library contains the main F#'s monads found on FSharp.Core lib written in, and adapted for, C#.
Installing
The project's package can be found on Nuget and installed by your IDE or shell as following:
dotnet add package Moonad
or
PM> Install-Package Moonad
A Note on Null Reference Types
Since our main goal is to protect the user from NullReferenceException
we strongly recommend the use of Nullable Reference Types on any project which uses this lib.
The Monads
F# offers in it's core library four monads to help you to have more flexibility when working with primitives and also potential null occurrences. So this library do the same.
Choice
Also known as Either
in some languages this monad offers you the possibility to choose one of two types to be hold by its instance.
Example:
public Choice<int, string> Choose(bool returnInt)
{
if(returnInt)
return 1;
return "This is a Choice!";
}
Result
A type to express the final state of a given processing revealing its success of failure and optionally carrying a value or an error.
Example 1 - Success indicator:
public Result Send(Message message)
{
try
{
...
return Result.Sucess();
}
catch(Exception exc)
{
...
return Result.Failure();
}
}
Example 2 - Value and error returning:
public Result<User, IError> Create(...)
{
//When a guard clause is actioned
return new EmptyUserNameError();
//When all is valid
return new User(...);
}
Option
This monad, also known as Maybe
, has as its goal preventing the NullReferenceException
by notifying the existence or absense of a value. Once a potentially null, or simply absent, value is converted to Option it's evaluated to a Some
instance, which carry the value, or a None
instance, which replaces the null
and let the client works as null
doesn't exists.
Example 1 - Preventing null from a 3rd party lib:
//lib.Method returns a string
var option = lib.Method().ToOption();
//The ToOption method will turn a null value into a None instance.
if(option.IsSome)
Console.WriteLine($"Returned value: {option}");
if(option.IsNone)
Console.WriteLine($"No returned value.");
Example 2 - Creating an Option explicitly:
public Option<int> ReturnWhenGreaterThanZero(int input) =>
input > 0 ? input : Option<int>.None;
ValueOption
It has the very same concept as Option but is intended to use with value types to be faster in performance critical scenarios.
Example 1 - Preventing null from a 3rd party lib:
//lib.Method returns a nullable int
var option = lib.Method().ToValueOption();
//The ToOption method will turn a null value into a None instance.
if(option.IsSome)
Console.WriteLine($"Returned value: {option}");
if(option.IsNone)
Console.WriteLine($"No returned value.");
Example 2 - Creating an Option explicitly:
public ValueOption<int> ReturnWhenGreaterThanZero(int input) =>
input > 0 ? input : ValueOption<int>.None;
This guide is yet to be finalized
We ask for your patience while we still develop the docs about all the resources for each monad available on this lib. While waiting it's possible to check the source code and the F# docs to have an idea on how this lib works.
Thanks!
Links
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.