Skip to content

Instantly share code, notes, and snippets.

@happy-bracket
Created September 21, 2019 13:16
Show Gist options
  • Save happy-bracket/cec58ab69cf78490e846216568758c23 to your computer and use it in GitHub Desktop.
Save happy-bracket/cec58ab69cf78490e846216568758c23 to your computer and use it in GitHub Desktop.
Either, but in Dart
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