Moonad 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Moonad --version 1.0.2                
NuGet\Install-Package Moonad -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Moonad" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Moonad --version 1.0.2                
#r "nuget: Moonad, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Moonad as a Cake Addin
#addin nuget:?package=Moonad&version=1.0.2

// Install Moonad as a Cake Tool
#tool nuget:?package=Moonad&version=1.0.2                

Moonad

GitHub Actions Tests NuGet

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# on top of netstandard2.1.

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 the infamous 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 operation 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 bag:

public Result<User> Create(...)
{
    //When a guard clause is actioned
    return Result<User>.Failure();

    //When all is valid
    return new User(...);
}

Example 3 - Value and Error bag

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. Once a potentially null 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 it 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 of 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!

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last updated
5.2.1 61 11/3/2024
5.2.0 595 4/29/2024
5.1.0 109 4/15/2024
5.0.1 107 3/29/2024
5.0.0 107 3/29/2024
4.0.0 103 2/18/2024
3.0.1 148 1/13/2024
3.0.0 105 1/13/2024
2.1.0 134 12/26/2023
2.0.0 97 12/26/2023
1.0.2 129 12/25/2023
1.0.1 104 12/24/2023
1.0.0 126 12/24/2023