🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

nomads

Package Overview
Dependencies
Maintainers
0
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

nomads

0.1.0-alpha.6458712013
unpublished
NuGet
Maintainers
0
Created
Source

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.

[!NOTE] 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

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");

[!NOTE] Primitives None, Ok and Error records are primarily used for easy implicit casting in cases types are ambiguous or aligns with user preferences.

Resolving values

Both Option and Result expose a public HasValue property that can be queried to determine valid access to the (also public) Value property.

Option<string> some = Option.Some("Hey");
string value = some.HasValue 
    ? some.Value! 
    : "???";

Result<string, Exception> result = new Exception("I failed you");
string output = result.HasValue 
    ? result.Value 
    : result.Error!.Message;

Optionally and for convenience, extension methods are provided.

Option<string> value = Option
    .Some("Hey")
    .ValueOrElse("???");

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

Map (aka Select)

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

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

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

The word Select is used to defined what is commonly known in FP as map operation, being Select is more akin to C# syntax.

Resources

License

This project is licensed with the MIT license.

Footnotes

Keywords

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts