|
1 | 1 | package com.jnape.palatable.lambda.functions.builtin.fn2; |
2 | 2 |
|
3 | | -import com.jnape.palatable.lambda.functions.Fn1; |
4 | 3 | import com.jnape.palatable.lambda.functions.Fn2; |
| 4 | +import com.jnape.palatable.lambda.functions.specialized.Kleisli; |
5 | 5 | import com.jnape.palatable.lambda.functor.builtin.Lazy; |
6 | 6 |
|
| 7 | +import static com.jnape.palatable.lambda.functions.specialized.Kleisli.kleisli; |
7 | 8 | import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy; |
8 | | -import static com.jnape.palatable.lambda.monad.Monad.join; |
9 | 9 |
|
10 | 10 | /** |
11 | 11 | * Given a {@link Fn2} that receives a recursive function and an input and yields a {@link Lazy lazy} result, and an |
|
26 | 26 | * @param <A> the input type |
27 | 27 | * @param <B> the output type |
28 | 28 | */ |
29 | | -public final class LazyRec<A, B> implements Fn2<Fn2<Fn1<? super A, ? extends Lazy<B>>, A, Lazy<B>>, A, Lazy<B>> { |
| 29 | +public final class LazyRec<A, B> implements |
| 30 | + Fn2<Fn2<Kleisli<? super A, ? extends B, Lazy<?>, Lazy<B>>, A, Lazy<B>>, A, Lazy<B>> { |
30 | 31 |
|
31 | 32 | private static final LazyRec<?, ?> INSTANCE = new LazyRec<>(); |
32 | 33 |
|
33 | 34 | private LazyRec() { |
34 | 35 | } |
35 | 36 |
|
36 | 37 | @Override |
37 | | - public Lazy<B> checkedApply(Fn2<Fn1<? super A, ? extends Lazy<B>>, A, Lazy<B>> fn, A a) { |
38 | | - return join(lazy(() -> fn.apply(nextA -> apply(fn, nextA), a))); |
| 38 | + public Lazy<B> checkedApply(Fn2<Kleisli<? super A, ? extends B, Lazy<?>, Lazy<B>>, A, Lazy<B>> fn, A a) { |
| 39 | + return lazy(a).flatMap(fn.apply(lazyRec(fn))); |
39 | 40 | } |
40 | 41 |
|
41 | 42 | @SuppressWarnings("unchecked") |
42 | 43 | public static <A, B> LazyRec<A, B> lazyRec() { |
43 | 44 | return (LazyRec<A, B>) INSTANCE; |
44 | 45 | } |
45 | 46 |
|
46 | | - public static <A, B> Fn1<A, Lazy<B>> lazyRec(Fn2<Fn1<? super A, ? extends Lazy<B>>, A, Lazy<B>> fn) { |
47 | | - return LazyRec.<A, B>lazyRec().apply(fn); |
| 47 | + public static <A, B> Kleisli<? super A, ? extends B, Lazy<?>, Lazy<B>> lazyRec( |
| 48 | + Fn2<Kleisli<? super A, ? extends B, Lazy<?>, Lazy<B>>, A, Lazy<B>> fn) { |
| 49 | + return kleisli(LazyRec.<A, B>lazyRec().apply(fn)); |
48 | 50 | } |
49 | 51 |
|
50 | | - public static <A, B> Lazy<B> lazyRec(Fn2<Fn1<? super A, ? extends Lazy<B>>, A, Lazy<B>> fn, A a) { |
| 52 | + public static <A, B> Lazy<B> lazyRec(Fn2<Kleisli<? super A, ? extends B, Lazy<?>, Lazy<B>>, A, Lazy<B>> fn, A a) { |
51 | 53 | return lazyRec(fn).apply(a); |
52 | 54 | } |
53 | 55 | } |
0 commit comments