Skip to content

Instantly share code, notes, and snippets.

@nachivpn
Last active July 30, 2017 18:33
Show Gist options
  • Save nachivpn/be7d044bbbbaf0ee357f9d8eec3f434e to your computer and use it in GitHub Desktop.
Save nachivpn/be7d044bbbbaf0ee357f9d8eec3f434e to your computer and use it in GitHub Desktop.
LeftRight.java
public final static class Left<LL, LR> extends Either<LL, LR>{
LL l;
public Left(LL l) {
this.l = l;
}
@Override
public <T> T match(Function<LL, T> a, Function<LR, T> b) {
return a.apply(l);
}
@Override
public <S> Either<LL, S> bind(Function<LR, Either<LL, S>> f) {
//f cannot be applied, simply return Left value for new Either type
return new Left<>(l);
}
@Override
public <S> Either<LL, S> fmap(Function<LR, S> f) {
//f cannot be applied, simply return Left value for new Either type
return new Left<>(l);
}
}
public final static class Right<RL,RR> extends Either<RL, RR>{
RR r;
public Right(RR r) {
this.r = r;
}
@Override
public <T> T match(Function<RL, T> a, Function<RR, T> b) {
return b.apply(r);
}
@Override
public <S> Either<RL, S> bind(Function<RR, Either<RL, S>> f) {
//RR value available, apply f!
return f.apply(r);
}
@Override
public <S> Either<RL, S> fmap(Function<RR, S> f) {
//RR value available, apply f & make an new Right value for new Either type
return new Right<>(f.apply(r));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment