Skip to content

alvpickmans/nomads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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.

Footnotes

  1. Railway Oriented Programming by Scott Wlaschin

About

(No) Monadic constructs for Csharp

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

Generated from dotnet/new-repo