|
| 1 | +package com.jnape.palatable.lambda.functions; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.Unit; |
| 4 | +import com.jnape.palatable.lambda.functor.Applicative; |
| 5 | +import com.jnape.palatable.lambda.monad.Monad; |
| 6 | + |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.function.Supplier; |
| 9 | + |
| 10 | +import static com.jnape.palatable.lambda.adt.Unit.UNIT; |
| 11 | + |
| 12 | +/** |
| 13 | + * A function taking "no arguments", implemented as an <code>{@link Fn1}<{@link Unit}, A></code>. |
| 14 | + * |
| 15 | + * @param <A> the result type |
| 16 | + * @see Fn1 |
| 17 | + * @see Supplier |
| 18 | + */ |
| 19 | +@FunctionalInterface |
| 20 | +public interface Fn0<A> extends Fn1<Unit, A>, Supplier<A> { |
| 21 | + |
| 22 | + /** |
| 23 | + * Invoke this function with {@link Unit}. |
| 24 | + * |
| 25 | + * @param unit the only allowed input |
| 26 | + * @return the result value |
| 27 | + */ |
| 28 | + @Override |
| 29 | + A apply(Unit unit); |
| 30 | + |
| 31 | + /** |
| 32 | + * Apply this {@link Fn0}, supplying {@link Unit} as the input. |
| 33 | + * |
| 34 | + * @return the output |
| 35 | + */ |
| 36 | + default A apply() { |
| 37 | + return apply(UNIT); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + default <B> Fn0<B> flatMap(Function<? super A, ? extends Monad<B, Fn1<Unit, ?>>> f) { |
| 42 | + return Fn1.super.flatMap(f)::apply; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + default <B> Fn0<B> fmap(Function<? super A, ? extends B> f) { |
| 47 | + return Fn1.super.fmap(f)::apply; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + default <B> Fn0<B> pure(B b) { |
| 52 | + return Fn1.super.pure(b)::apply; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + default <B> Fn0<B> zip(Applicative<Function<? super A, ? extends B>, Fn1<Unit, ?>> appFn) { |
| 57 | + return Fn1.super.zip(appFn)::apply; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + default <B> Fn0<B> zip(Fn2<Unit, A, B> appFn) { |
| 62 | + return Fn1.super.zip(appFn)::apply; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + default <B> Fn0<B> discardL(Applicative<B, Fn1<Unit, ?>> appB) { |
| 67 | + return Fn1.super.discardL(appB)::apply; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + default <B> Fn0<A> discardR(Applicative<B, Fn1<Unit, ?>> appB) { |
| 72 | + return Fn1.super.discardR(appB)::apply; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + default <B> Fn0<B> diMapR(Function<? super A, ? extends B> fn) { |
| 77 | + return Fn1.super.diMapR(fn)::apply; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + default <Z> Fn1<Z, A> compose(Function<? super Z, ? extends Unit> before) { |
| 82 | + return Fn1.super.compose(before)::apply; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + default <B> Fn0<B> andThen(Function<? super A, ? extends B> after) { |
| 87 | + return Fn1.super.andThen(after)::apply; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + default A get() { |
| 92 | + return apply(UNIT); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Convenience method for converting a {@link Supplier} to an {@link Fn0}. |
| 97 | + * |
| 98 | + * @param supplier the supplier |
| 99 | + * @param <A> the output type |
| 100 | + * @return the {@link Fn0} |
| 101 | + */ |
| 102 | + static <A> Fn0<A> fn0(Supplier<A> supplier) { |
| 103 | + return __ -> supplier.get(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Static factory method for coercing a lambda to an {@link Fn0}. |
| 108 | + * |
| 109 | + * @param fn the lambda to coerce |
| 110 | + * @param <A> the output type |
| 111 | + * @return the {@link Fn0} |
| 112 | + */ |
| 113 | + static <A> Fn0<A> fn0(Fn0<A> fn) { |
| 114 | + return fn; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Static factory method for adapting a {@link Runnable} to an <code>{@link Fn0}<{@link Unit}></code>. |
| 119 | + * |
| 120 | + * @param fn the {@link Runnable} |
| 121 | + * @return the {@link Fn0} |
| 122 | + */ |
| 123 | + static Fn0<Unit> fn0(Runnable fn) { |
| 124 | + return unit -> { |
| 125 | + fn.run(); |
| 126 | + return unit; |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Static factory method for adapting a {@link Function} to an {@link Fn0}. |
| 132 | + * |
| 133 | + * @param fn the {@link Function} |
| 134 | + * @param <A> the output type |
| 135 | + * @return the {@link Fn0} |
| 136 | + */ |
| 137 | + static <A> Fn0<A> fn0(Function<Unit, A> fn) { |
| 138 | + return fn0(() -> fn.apply(UNIT)); |
| 139 | + } |
| 140 | +} |
0 commit comments