6
6
import com .jnape .palatable .lambda .adt .hlist .HList ;
7
7
import com .jnape .palatable .lambda .adt .hlist .Tuple3 ;
8
8
import com .jnape .palatable .lambda .functions .Fn1 ;
9
+ import com .jnape .palatable .lambda .functions .recursion .RecursiveResult ;
9
10
import com .jnape .palatable .lambda .functions .specialized .Pure ;
10
11
import com .jnape .palatable .lambda .functor .Applicative ;
11
12
import com .jnape .palatable .lambda .functor .Bifunctor ;
12
13
import com .jnape .palatable .lambda .functor .Functor ;
13
14
import com .jnape .palatable .lambda .functor .builtin .Lazy ;
14
15
import com .jnape .palatable .lambda .monad .Monad ;
16
+ import com .jnape .palatable .lambda .monad .MonadRec ;
15
17
import com .jnape .palatable .lambda .traversable .Traversable ;
16
18
17
19
import java .util .Objects ;
18
20
19
21
import static com .jnape .palatable .lambda .functions .builtin .fn2 .Into3 .into3 ;
22
+ import static com .jnape .palatable .lambda .functions .recursion .RecursiveResult .terminate ;
23
+ import static com .jnape .palatable .lambda .functions .recursion .Trampoline .trampoline ;
20
24
import static com .jnape .palatable .lambda .functor .builtin .Lazy .lazy ;
21
25
22
26
/**
@@ -32,7 +36,19 @@ public abstract class Choice3<A, B, C> implements
32
36
CoProduct3 <A , B , C , Choice3 <A , B , C >>,
33
37
Monad <C , Choice3 <A , B , ?>>,
34
38
Bifunctor <B , C , Choice3 <A , ?, ?>>,
35
- Traversable <C , Choice3 <A , B , ?>> {
39
+ Traversable <C , Choice3 <A , B , ?>>,
40
+ MonadRec <C , Choice3 <A , B , ?>> {
41
+
42
+ /**
43
+ * {@inheritDoc}
44
+ */
45
+ @ Override
46
+ public <D > Choice3 <A , B , D > trampolineM (Fn1 <? super C , ? extends MonadRec <RecursiveResult <C , D >, Choice3 <A , B , ?>>> fn ) {
47
+ return flatMap (trampoline (c -> fn .apply (c ).<Choice3 <A , B , RecursiveResult <C , D >>>coerce ()
48
+ .match (a -> terminate (a (a )),
49
+ b -> terminate (b (b )),
50
+ r -> r .fmap (Choice3 ::c ))));
51
+ }
36
52
37
53
private Choice3 () {
38
54
}
@@ -68,7 +84,7 @@ public final Choice2<A, B> converge(Fn1<? super C, ? extends CoProduct2<A, B, ?>
68
84
*/
69
85
@ Override
70
86
public final <D > Choice3 <A , B , D > fmap (Fn1 <? super C , ? extends D > fn ) {
71
- return Monad .super .<D >fmap (fn ).coerce ();
87
+ return MonadRec .super .<D >fmap (fn ).coerce ();
72
88
}
73
89
74
90
/**
@@ -109,7 +125,7 @@ public <D> Choice3<A, B, D> pure(D d) {
109
125
*/
110
126
@ Override
111
127
public <D > Choice3 <A , B , D > zip (Applicative <Fn1 <? super C , ? extends D >, Choice3 <A , B , ?>> appFn ) {
112
- return Monad .super .zip (appFn ).coerce ();
128
+ return MonadRec .super .zip (appFn ).coerce ();
113
129
}
114
130
115
131
/**
@@ -128,15 +144,15 @@ public <D> Lazy<Choice3<A, B, D>> lazyZip(
128
144
*/
129
145
@ Override
130
146
public <D > Choice3 <A , B , D > discardL (Applicative <D , Choice3 <A , B , ?>> appB ) {
131
- return Monad .super .discardL (appB ).coerce ();
147
+ return MonadRec .super .discardL (appB ).coerce ();
132
148
}
133
149
134
150
/**
135
151
* {@inheritDoc}
136
152
*/
137
153
@ Override
138
154
public <D > Choice3 <A , B , C > discardR (Applicative <D , Choice3 <A , B , ?>> appB ) {
139
- return Monad .super .discardR (appB ).coerce ();
155
+ return MonadRec .super .discardR (appB ).coerce ();
140
156
}
141
157
142
158
/**
0 commit comments