Implementation of Haskell's Either type in C#
You should install EitherResult with NuGet
Install-Package Arax.EitherResult
Or via the .NET Core command line interface:
dotnet add package Arax.EitherResult
public Either<int,string> GetResult()
{
if (condition)
{
return Either<int, string>.Left(1);
}
else
{
return Either<int, string>.Right("Result");
}
}
public Either<int,string> GetPerson(long id)
{
if (condition)
{
return 1;
}
else
{
return "Result";
}
}
public void UseResult()
{
Either<int, string> result = GetResult();
result.Switch(onLeft: left => { },
onRight: right => { });
Either<string, int> mapResult = result.Map(transformLeft:left => "map left",
transformRight: right => 25);
string foldResult = result.Fold(transformLeft: left => left.ToString(),
transformRight: right => right);
}