Last active
March 23, 2020 07:25
-
-
Save dwickstrom/2399bf107a057cdc8a4fb4e936c9ae76 to your computer and use it in GitHub Desktop.
Scott encoded Either
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
package se.foo.bar; | |
import java.util.Optional; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
import java.util.stream.Stream; | |
interface Either<L, R> { | |
<T> T either(Function<L, T> left, Function<R, T> right); | |
<T> Either<L, T> map(Function<R, T> mapper); | |
<T> Either<L, T> flatMap(Function<R, Either<L, T>> fn); | |
boolean isLeft(); | |
boolean isRight(); | |
Stream<R> toStream(); | |
Optional<R> toOptional(); | |
R orElse(R other); | |
R orElseGet(Supplier<R> other); | |
L fromLeft(L l); | |
R fromRight(R r); | |
static <T> Either<Throwable, T> tryCatch(Supplier<T> supplier) { | |
try { | |
return Either.Right(supplier.get()); | |
} catch (Throwable t) { | |
return Either.Left(t); | |
} | |
} | |
static <L, R> Either<L, R> Left(L a) { | |
return new Either<L, R>() { | |
@Override | |
public <C> C either(Function<L, C> l, Function<R, C> r) { | |
return l.apply(a); | |
} | |
@Override | |
public <T> Either<L, T> map(Function<R, T> mapper) { | |
return Left(a); | |
} | |
@Override | |
public <T> Either<L, T> flatMap(Function<R, Either<L, T>> fn) { | |
return Left(a); | |
} | |
@Override | |
public boolean isLeft() { | |
return true; | |
} | |
@Override | |
public boolean isRight() { | |
return false; | |
} | |
@Override | |
public Stream<R> toStream() { | |
return Stream.empty(); | |
} | |
@Override | |
public Optional<R> toOptional() { | |
return Optional.empty(); | |
} | |
@Override | |
public String toString() { | |
return String.format("Left(%s)", a); | |
} | |
public R orElse(R other) { | |
return other; | |
} | |
@Override | |
public R orElseGet(Supplier<R> other) { | |
return other.get(); | |
} | |
@Override | |
public L fromLeft(L __) { | |
return a; | |
} | |
@Override | |
public R fromRight(R r) { | |
return r; | |
} | |
}; | |
} | |
static <L, R> Either<L, R> Right(R b) { | |
return new Either<L, R>() { | |
@Override | |
public <T> T either(Function<L, T> l, Function<R, T> r) { | |
return r.apply(b); | |
} | |
@Override | |
public <T> Either<L, T> map(Function<R, T> fn) { | |
return Right(fn.apply(b)); | |
} | |
@Override | |
public <T> Either<L, T> flatMap(Function<R, Either<L, T>> fn) { | |
return fn.apply(b); | |
} | |
@Override | |
public boolean isLeft() { | |
return false; | |
} | |
@Override | |
public boolean isRight() { | |
return true; | |
} | |
@Override | |
public Stream<R> toStream() { | |
return Stream.of(b); | |
} | |
@Override | |
public Optional<R> toOptional() { | |
return Optional.of(b); | |
} | |
@Override | |
public R orElse(R __) { | |
return b; | |
} | |
@Override | |
public R orElseGet(Supplier<R> __) { | |
return b; | |
} | |
@Override | |
public L fromLeft(L l) { | |
return l; | |
} | |
@Override | |
public R fromRight(R __) { | |
return b; | |
} | |
@Override | |
public String toString() { | |
return String.format("Right(%s)", b); | |
} | |
}; | |
} | |
} |
Author
dwickstrom
commented
Mar 21, 2020
•
e instanceof Right
won't work though :/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment