Skip to content

Instantly share code, notes, and snippets.

@dimitris-papadimitriou-chr
Created June 17, 2019 21:02
Show Gist options
  • Save dimitris-papadimitriou-chr/398f185251e543c42199c731bd36fd97 to your computer and use it in GitHub Desktop.
Save dimitris-papadimitriou-chr/398f185251e543c42199c731bd36fd97 to your computer and use it in GitHub Desktop.
public abstract class Either<TLeft, TRight>
{
public abstract void IfLeft(Action<TLeft> action);
public abstract void IfRight(Action<TRight> action);
public abstract Either<TLeft, T1Right> Select<T1Right>(Func<TRight, T1Right> mapping);
public abstract TResult Match<TResult>(Func<TLeft, TResult> Left, Func<TRight, TResult> Right);
}
public class Left<TLeft, TRight> : Either<TLeft, TRight>
{
private readonly TLeft value;
public Left(TLeft left)
{
this.value = left;
}
public override void IfLeft(Action<TLeft> action)
{
action(value);
}
public override void IfRight(Action<TRight> action) { }
public override TResult Match<TResult>(Func<TLeft, TResult> Left, Func<TRight, TResult> Right)
{
return Left(value);
}
public override Either<TLeft, T1Right> Select<T1Right>(Func<TRight, T1Right> mapping)
{
return new Left<TLeft, T1Right>(value);
}
}
public class Right<TLeft, TRight> : Either<TLeft, TRight>
{
private readonly TRight value;
public Right(TRight value)
{
this.value = value;
}
public override void IfLeft(Action<TLeft> action) { }
public override void IfRight(Action<TRight> action)
{
action(value);
}
public override Either<TLeft, T1Right> Select<T1Right>(Func<TRight, T1Right> mapping)
{
return new Right<TLeft, T1Right>(mapping(value));
}
public override TResult Match<TResult>(Func<TLeft, TResult> Left, Func<TRight, TResult> Right)
{
return Right(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment