Created
March 14, 2019 22:38
-
-
Save mchmielarz/75bba8e18d44d909cc3e3d88d2eef4ad to your computer and use it in GitHub Desktop.
for-comprehension: For-comprehension example with three Option suppliers
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
private static <T1, T2, T3> ForSupplier3Option<T1, T2, T3> For(Supplier<Option<T1>> ts1, | |
Supplier<Option<T2>> ts2, | |
Supplier<Option<T3>> ts3) { | |
return new ForSupplier3Option<>(ts1, ts2, ts3); | |
} | |
public static class ForSupplier3Option<T1, T2, T3> { | |
private final Supplier<Option<T1>> ts1; | |
private final Supplier<Option<T2>> ts2; | |
private final Supplier<Option<T3>> ts3; | |
private ForSupplier3Option(Supplier<Option<T1>> ts1, Supplier<Option<T2>> ts2, Supplier<Option<T3>> ts3) { | |
this.ts1 = ts1; | |
this.ts2 = ts2; | |
this.ts3 = ts3; | |
} | |
public <R> Option<R> yield(Function3<? super T1, ? super T2, ? super T3, ? extends R> f) { | |
Objects.requireNonNull(f, "f is null"); | |
return | |
ts1.get().flatMap(t1 -> | |
ts2.get().flatMap(t2 -> | |
ts3.get().map(t3 -> f.apply(t1, t2, t3)))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment