1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| import io.vavr.control.Either;
import java.util.concurrent.ThreadLocalRandom;
public class Eithers {
private static ThreadLocalRandom random =
ThreadLocalRandom.current();
public static void main(String[] args) {
Either<String, String> either = compute()
.map(str -> str + " World")
.mapLeft(Throwable::getMessage);
System.out.println(either);
}
private static Either<Throwable, String> compute() {
return random.nextBoolean()
? Either.left(new RuntimeException("Boom!"))
: Either.right("Hello");
}
}
|