araxis / EitherResult

Implementation of Haskell's Either type in C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EitherResult

Arax.EitherResult - NuGet NuGet .NET

Implementation of Haskell's Either type in C#

Installing EitherResult

You should install EitherResult with NuGet

Install-Package Arax.EitherResult

Or via the .NET Core command line interface:

dotnet add package Arax.EitherResult

Make Either<T,U> and Return

    public Either<int,string> GetResult()
    {
      if (condition)
      {
         return Either<int, string>.Left(1);
      }
      else
      {
         return Either<int, string>.Right("Result");
      }
    }

OR : More Simple

public Either<int,string> GetPerson(long id)
{
   if (condition)
   {
      return 1;
   }
   else
   {
      return "Result";
   }
 }

Usage

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

About

Implementation of Haskell's Either type in C#


Languages

Language:C# 100.0%