Nomads 0.1.0-alpha.23

This is a prerelease version of Nomads.
dotnet add package Nomads --version 0.1.0-alpha.23
                    
NuGet\Install-Package Nomads -Version 0.1.0-alpha.23
                    
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="Nomads" Version="0.1.0-alpha.23" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nomads" Version="0.1.0-alpha.23" />
                    
Directory.Packages.props
<PackageReference Include="Nomads" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nomads --version 0.1.0-alpha.23
                    
#r "nuget: Nomads, 0.1.0-alpha.23"
                    
#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.
#addin nuget:?package=Nomads&version=0.1.0-alpha.23&prerelease
                    
Install Nomads as a Cake Addin
#tool nuget:?package=Nomads&version=0.1.0-alpha.23&prerelease
                    
Install Nomads as a Cake Tool

Nomads

Exploring monads from the illiterate perspective of a software engineer that has no clue about hardcore mathematics, functional programming or category theory.

Motivation

At some point at work, I was introduced to Railway Oriented Programming [^1] by a colleague that created an internal library to chain and beautifully create workflows or logic steps. Although descriptive enough, the simple difference between Bind() and Map() caught me unprepared and concerns were raised about how user friendly it would to OOP die-hard. We settled on predominantly having a Then() method with overloads for when it acted as a bind or map, and I cannot stress enough how pleasant is to write code with it.

In parallel, I started to sporadically dabble into Rust and soon fell in love with the no such thing as null feature and its Result and Option types, which after so many Object null exceptions in csharp, it felt like a breeze of fresh air and paradoxically the right way to handle not having a value.

This project is an attempt to slowly understand functional programming concepts while at the same time implementing a utility library I'll be comfortable using. Focusing on practicality on C# rather that functional purism, this will aim to bridge the gap between engineering and theory providing familiar constructs for the c# connoisseur.

This project will might not be exactly what a Category Theory/Monads expert expect. There is an excellent project, language-ext that basically brings almost all FP paradigms to c#. Nomads is a extremely low-fi version that looks to start as close as possible to C#, aiming to reduce "agnosiophobia" on the newcomers.

Why "Nomads"?

I think like everyone, I struggled (and still do) with the concept of Monads. Misspelling it is my tribute to that sensation of feeling dumb while it seems everyone with an article on Monads is enlighten with a divine secret.

Also, it sounds kinda cute.

Usage

Functors (aka Generics)

Nomads provide implementations for the basic functional functors Option<T> and Result<TValue, TError>.

Instancing

Inheritance types Constructors

using Nomads;

Option<string> someOption = new Some<string>("Hi");
Option<string> noneOption = new None<string>();

Result<string, int> okResult = new Ok<string, int>("Ok");
Result<string, int> okResult = new Error<string, int>(-1);

Static Constructors

using Nomads;

Option<string> someOption = Option.Some("Hi");
Option<string> noneOption = Option.None();

Result<string, int> okResult = Result.Ok("Ok");
Result<string, int> okResult = Result.Error(-1);

Adding a static using statement simplifies it (try global usings!)

using Nomads;
using static Nomads.Option;
using static Nomads.Result;

Option<string> someOption = Some("Hi");
Option<string> noneOption = None();

Result<string, int> okResult = Ok("Ok");
Result<string, int> okResult = Error(-1);

Implicit casting

using Nomads;
using Nomads.Primitives;

Option<string> someOption = "Hi";
Option<string> noneOption = new None();

Result<string, int> okResult = "Ok";
Result<string, int> okResult = -1;

// For results where value and error are of the same type,
// specific Ok() or Error() primitives must be used.
Result<string, string> sameTypeOkResult = new Ok("Ok");
Result<string, string> sameTypeErrorResult = new Error("Err");

Reduce

To safely retrieve inner value (or error for Results), the Reduce() method allows to define a delegate so that:

  • It can be used instead in cases where Option is empty
  • Allows to handle the error scenario (or transform the result's value to another type)
string value = Option
    .Some("Hey")
    .Reduce("???");

string result = Error<string, Exception>(new Exception("I failed you"))
    .Reduce(
        ok => ok,
        err => err.Message
    );

Map (aka Select)

Both Option and Result functors can be "mapped" with function delegates using Map() extension methods.

var option = Option
    .Some("hi")
    .Map(x => x.ToUpper());
Assert.Equal("HI", option.Value!);

var result = Result
    .Ok("bye")
    .Map(x => x.ToUpper());
Assert.Equal("BYE", result.Value!);

Resources

License

This project is licensed with the MIT license.

[^1]: Railway Oriented Programming by Scott Wlaschin

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • 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
0.1.0-alpha.23 96 12/16/2023
0.1.0-alpha.22 88 12/16/2023
0.1.0-alpha.20 87 12/16/2023
0.1.0-alpha.17 77 12/13/2023
0.1.0-alpha.16 82 12/11/2023
0.1.0-alpha.15 82 10/9/2023
0.1.0-alpha.13 80 10/9/2023