Skip to content

Instantly share code, notes, and snippets.

@arialdomartini
Last active December 22, 2023 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arialdomartini/7846b51f919801baf0dcc1d80957d305 to your computer and use it in GitHub Desktop.
Save arialdomartini/7846b51f919801baf0dcc1d80957d305 to your computer and use it in GitHub Desktop.
paolilleither.cs
internal interface Either<L, R>
{
Either<L, B> Bind<B>(Func<R, Either<L, B>> f);
B Match<B>(Func<L, B> whenLeft, Func<R, B> whenRight);
}
internal sealed record Right<L, R> : Either<L, R>
{
private readonly R _right;
internal static Either<L, R> Return(R r) => new Right<L, R>(r);
private Right(R right)
{
_right = right;
}
Either<L, B> Either<L, R>.Bind<B>(Func<R, Either<L, B>> f) => f(_right);
B Either<L, R>.Match<B>(Func<L, B> _, Func<R, B> whenRight) => whenRight(_right);
}
internal sealed record Left<L, R> : Either<L, R>
{
private readonly L _left;
internal Left(L left)
{
_left = left;
}
Either<L, B> Either<L, R>.Bind<B>(Func<R, Either<L, B>> _) =>
new Left<L, B>(_left);
B Either<L, R>.Match<B>(Func<L, B> whenLeft, Func<R, B> whenRight) =>
whenLeft(_left);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment