Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Latest commit

 

History

History
62 lines (34 loc) · 1.56 KB

README.md

File metadata and controls

62 lines (34 loc) · 1.56 KB

Tp.Core.Functional

Tp.Core.Functional contains implementations of Maybe monad, Try monad and Either type.

Note for Github users

Primary source-of-truth repository is hosted on internal GitLab. Github repository is a read-only mirror.

Maybe monad

Maybe monad is an implementation of Option type. It represents encapsulation of an optional value; e.g. it is used as the return type of functions which may or may not return a meaningful value when they are applied.

Usages:

var i = Maybe.Just(1); // Create a value
var m = i.Select(x=>x*2); // map value, result is Just(2)
var f = m.Where(x=>x%2==1); // filter value, result is Nothing
var n = m.Bind(x=>Maybe.Just(2*x)) // flatMap (or bind) value, result is Just(4)

Tp.Functional.Core does not require to specify a type of Maybe.Nothing:

Maybe<int> n = Maybe.Nothing;
Maybe<int> m = Maybe<int>.Nothing;
n == m; // True

See more here

Either type

Either type represents disjoint union of two values:

var left = Either.CreateLeft<int,string>(1);
var right = Either.CreateRight<int,string>("string");

See more here

Try monad

A Try wraps a computation that could either result in a value or in an exception being thrown:

var tryResult = Try.Create(()=>int.Parse(value));

var m = try.Select(x=>x*2);

See more here