3772 |
|
return completedFuture(value); |
3773 |
|
} |
3774 |
|
// monadic zero ? |
3775 |
< |
static <T> CompletableFuture<T> zero(T value) { |
3775 |
> |
static <T> CompletableFuture<T> zero() { |
3776 |
|
return failedFuture(new MonadError()); |
3777 |
|
} |
3778 |
|
// >=> |
3818 |
|
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3819 |
|
CompletableFuture<? extends T> g) { |
3820 |
|
PlusFuture<T> plus = new PlusFuture<T>(); |
3821 |
< |
BiConsumer<T, Throwable> action = (T result, Throwable fail) -> { |
3821 |
> |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3822 |
|
if (result != null) { |
3823 |
|
if (plus.complete(result)) |
3824 |
|
if (plus.firstFailure.get() != null) |
3825 |
|
plus.firstFailure.set(null); |
3826 |
|
} |
3827 |
< |
else if (plus.firstFailure.compareAndSet(null, fail)) { |
3827 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3828 |
|
if (plus.isDone()) |
3829 |
|
plus.firstFailure.set(null); |
3830 |
|
} else { |
3831 |
< |
if (plus.completeExceptionally(fail)) |
3831 |
> |
if (plus.completeExceptionally(ex)) |
3832 |
|
plus.firstFailure.set(null); |
3833 |
|
} |
3834 |
|
}; |
3843 |
|
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
3844 |
|
*/ |
3845 |
|
public void testAdditiveMonad() throws Throwable { |
3846 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3847 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3848 |
+ |
|
3849 |
|
// Some mutually non-commutative functions |
3850 |
|
Function<Long, CompletableFuture<Long>> triple |
3851 |
< |
= (x) -> completedFuture(3 * x); |
3851 |
> |
= (x) -> Monad.unit(3 * x); |
3852 |
|
Function<Long, CompletableFuture<Long>> inc |
3853 |
< |
= (x) -> completedFuture(x + 1); |
3851 |
< |
|
3852 |
< |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3853 |
< |
Function<Long, CompletableFuture<Long>> zero = Monad::zero; |
3853 |
> |
= (x) -> Monad.unit(x + 1); |
3854 |
|
|
3855 |
|
// unit is a right identity: m >>= unit === m |
3856 |
< |
Monad.assertFutureEquals( |
3857 |
< |
inc.apply(5L).thenCompose(unit), |
3858 |
< |
inc.apply(5L)); |
3856 |
> |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
3857 |
> |
inc.apply(5L)); |
3858 |
|
// unit is a left identity: (unit x) >>= f === f x |
3859 |
< |
Monad.assertFutureEquals( |
3860 |
< |
unit.apply(5L).thenCompose(inc), |
3861 |
< |
inc.apply(5L)); |
3859 |
> |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
3860 |
> |
inc.apply(5L)); |
3861 |
> |
|
3862 |
|
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
3863 |
|
Monad.assertFutureEquals( |
3864 |
|
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
3865 |
|
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
3866 |
|
|
3867 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
3868 |
+ |
|
3869 |
|
// zero is a monadic zero |
3870 |
< |
Monad.assertZero(zero.apply(5L)); |
3870 |
< |
// left zero: zero >>= f === zero |
3871 |
< |
Monad.assertZero(zero.apply(5L).thenCompose(inc)); |
3872 |
< |
// right zero: f >>= zero === zero |
3873 |
< |
Monad.assertZero(inc.apply(5L).thenCompose(zero)); |
3870 |
> |
Monad.assertZero(zero); |
3871 |
|
|
3872 |
< |
// inc plus zero === inc |
3873 |
< |
Monad.assertFutureEquals( |
3874 |
< |
inc.apply(5L), |
3875 |
< |
Monad.plus(inc.apply(5L), zero.apply(5L))); |
3876 |
< |
// zero plus inc === inc |
3877 |
< |
Monad.assertFutureEquals( |
3878 |
< |
inc.apply(5L), |
3879 |
< |
Monad.plus(zero.apply(5L), inc.apply(5L))); |
3872 |
> |
// left zero: zero >>= f === zero |
3873 |
> |
Monad.assertZero(zero.thenCompose(inc)); |
3874 |
> |
// right zero: f >>= (\x -> zero) === zero |
3875 |
> |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
3876 |
> |
|
3877 |
> |
// f plus zero === f |
3878 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
3879 |
> |
Monad.plus(Monad.unit(5L), zero)); |
3880 |
> |
// zero plus f === f |
3881 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
3882 |
> |
Monad.plus(zero, Monad.unit(5L))); |
3883 |
|
// zero plus zero === zero |
3884 |
< |
Monad.assertZero(Monad.plus(zero.apply(5L), zero.apply(5L))); |
3884 |
> |
Monad.assertZero(Monad.plus(zero, zero)); |
3885 |
|
{ |
3886 |
< |
CompletableFuture<Long> f = Monad.plus(inc.apply(5L), inc.apply(8L)); |
3887 |
< |
assertTrue(f.get() == 6L || f.get() == 9L); |
3886 |
> |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
3887 |
> |
Monad.unit(8L)); |
3888 |
> |
// non-determinism |
3889 |
> |
assertTrue(f.get() == 5L || f.get() == 8L); |
3890 |
|
} |
3891 |
|
|
3892 |
|
CompletableFuture<Long> godot = new CompletableFuture<>(); |
3893 |
< |
// inc plus godot === inc (doesn't wait for godot) |
3894 |
< |
Monad.assertFutureEquals( |
3895 |
< |
inc.apply(5L), |
3896 |
< |
Monad.plus(inc.apply(5L), godot)); |
3897 |
< |
// godot plus inc === inc (doesn't wait for godot) |
3898 |
< |
Monad.assertFutureEquals( |
3897 |
< |
inc.apply(5L), |
3898 |
< |
Monad.plus(godot, inc.apply(5L))); |
3893 |
> |
// f plus godot === f (doesn't wait for godot) |
3894 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
3895 |
> |
Monad.plus(Monad.unit(5L), godot)); |
3896 |
> |
// godot plus f === f (doesn't wait for godot) |
3897 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
3898 |
> |
Monad.plus(godot, Monad.unit(5L))); |
3899 |
|
} |
3900 |
|
|
3901 |
|
// static <U> U join(CompletionStage<U> stage) { |