Skip to content

petrkoutnycz/Tp.Core.Functional

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Tp.Core.Functional

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

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

About

Implementation of Maybe, Try and Either

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%