Last active
December 22, 2023 09:18
-
-
Save arialdomartini/7846b51f919801baf0dcc1d80957d305 to your computer and use it in GitHub Desktop.
paolilleither.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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