6
6
import com .jnape .palatable .lambda .adt .hlist .HList ;
7
7
import com .jnape .palatable .lambda .adt .hlist .Tuple2 ;
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 .Into .into ;
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
/**
30
34
*/
31
35
public abstract class Choice2 <A , B > implements
32
36
CoProduct2 <A , B , Choice2 <A , B >>,
33
- Monad <B , Choice2 <A , ?>>,
37
+ MonadRec <B , Choice2 <A , ?>>,
34
38
Bifunctor <A , B , Choice2 <?, ?>>,
35
39
Traversable <B , Choice2 <A , ?>> {
36
40
@@ -68,7 +72,7 @@ public Choice2<B, A> invert() {
68
72
*/
69
73
@ Override
70
74
public final <C > Choice2 <A , C > fmap (Fn1 <? super B , ? extends C > fn ) {
71
- return Monad .super .<C >fmap (fn ).coerce ();
75
+ return MonadRec .super .<C >fmap (fn ).coerce ();
72
76
}
73
77
74
78
/**
@@ -109,14 +113,14 @@ public <C> Choice2<A, C> pure(C c) {
109
113
*/
110
114
@ Override
111
115
public <C > Choice2 <A , C > zip (Applicative <Fn1 <? super B , ? extends C >, Choice2 <A , ?>> appFn ) {
112
- return Monad .super .zip (appFn ).coerce ();
116
+ return MonadRec .super .zip (appFn ).coerce ();
113
117
}
114
118
115
119
/**
116
120
* {@inheritDoc}
117
121
*/
118
122
@ Override
119
- public <C > Lazy <? extends Monad < C , Choice2 <A , ?> >> lazyZip (
123
+ public <C > Lazy <Choice2 <A , C >> lazyZip (
120
124
Lazy <? extends Applicative <Fn1 <? super B , ? extends C >, Choice2 <A , ?>>> lazyAppFn ) {
121
125
return match (a -> lazy (a (a )),
122
126
b -> lazyAppFn .fmap (choiceF -> choiceF .<C >fmap (f -> f .apply (b )).coerce ()));
@@ -127,15 +131,15 @@ public <C> Choice2<A, C> zip(Applicative<Fn1<? super B, ? extends C>, Choice2<A,
127
131
*/
128
132
@ Override
129
133
public <C > Choice2 <A , C > discardL (Applicative <C , Choice2 <A , ?>> appB ) {
130
- return Monad .super .discardL (appB ).coerce ();
134
+ return MonadRec .super .discardL (appB ).coerce ();
131
135
}
132
136
133
137
/**
134
138
* {@inheritDoc}
135
139
*/
136
140
@ Override
137
141
public <C > Choice2 <A , B > discardR (Applicative <C , Choice2 <A , ?>> appB ) {
138
- return Monad .super .discardR (appB ).coerce ();
142
+ return MonadRec .super .discardR (appB ).coerce ();
139
143
}
140
144
141
145
/**
@@ -146,6 +150,15 @@ public final <C> Choice2<A, C> flatMap(Fn1<? super B, ? extends Monad<C, Choice2
146
150
return match (Choice2 ::a , b -> f .apply (b ).coerce ());
147
151
}
148
152
153
+ @ Override
154
+ public <C > Choice2 <A , C > trampolineM (Fn1 <? super B , ? extends MonadRec <RecursiveResult <B , C >, Choice2 <A , ?>>> fn ) {
155
+ return match (Choice2 ::a ,
156
+ trampoline (b -> fn .apply (b )
157
+ .<Choice2 <A , RecursiveResult <B , C >>>coerce ()
158
+ .match (a -> terminate (a (a )),
159
+ bOrC -> bOrC .fmap (Choice2 ::b ))));
160
+ }
161
+
149
162
/**
150
163
* {@inheritDoc}
151
164
*/
0 commit comments