Created
September 21, 2019 13:16
-
-
Save happy-bracket/cec58ab69cf78490e846216568758c23 to your computer and use it in GitHub Desktop.
Either, but in Dart
This file contains 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
typedef Function1<A, R> = R Function(A param); | |
class Either<L, R> { | |
bool _isRight; | |
L _left; | |
R _right; | |
C fold<C>(Function1<L, C> ifLeft, Function1<R, C> ifRight) { | |
if (_isRight) { | |
return ifRight(_right); | |
} else { | |
return ifLeft(_left); | |
} | |
} | |
static Either<L1, R1> left<L1, R1>(L1 value) { | |
Either<L1, R1> e = Either(); | |
e._left = value; | |
e._isRight = false; | |
return e; | |
} | |
static Either<L1, R1> right<L1, R1>(R1 value) { | |
Either<L1, R1> e = Either(); | |
e._right = value; | |
e._isRight = true; | |
return e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment