4
4
import com .jnape .palatable .lambda .adt .coproduct .CoProduct3 ;
5
5
import com .jnape .palatable .lambda .adt .hlist .Tuple2 ;
6
6
import com .jnape .palatable .lambda .functions .Fn1 ;
7
+ import com .jnape .palatable .lambda .functions .recursion .RecursiveResult ;
8
+ import com .jnape .palatable .lambda .functions .recursion .Trampoline ;
7
9
import com .jnape .palatable .lambda .functions .specialized .Pure ;
8
10
import com .jnape .palatable .lambda .functor .Applicative ;
9
11
import com .jnape .palatable .lambda .functor .Bifunctor ;
10
12
import com .jnape .palatable .lambda .functor .builtin .Lazy ;
11
13
import com .jnape .palatable .lambda .monad .Monad ;
14
+ import com .jnape .palatable .lambda .monad .MonadRec ;
12
15
import com .jnape .palatable .lambda .traversable .Traversable ;
13
16
14
17
import java .util .Objects ;
15
18
16
19
import static com .jnape .palatable .lambda .adt .hlist .HList .tuple ;
17
20
import static com .jnape .palatable .lambda .functions .builtin .fn1 .Constantly .constantly ;
18
21
import static com .jnape .palatable .lambda .functions .builtin .fn2 .Into .into ;
22
+ import static com .jnape .palatable .lambda .functions .builtin .fn2 .Sequence .sequence ;
19
23
import static com .jnape .palatable .lambda .functor .builtin .Lazy .lazy ;
20
24
21
25
/**
28
32
*/
29
33
public abstract class These <A , B > implements
30
34
CoProduct3 <A , B , Tuple2 <A , B >, These <A , B >>,
31
- Monad <B , These <A , ?>>,
35
+ MonadRec <B , These <A , ?>>,
32
36
Bifunctor <A , B , These <?, ?>>,
33
37
Traversable <B , These <A , ?>> {
34
38
@@ -58,6 +62,17 @@ public final <C> These<A, C> flatMap(Fn1<? super B, ? extends Monad<C, These<A,
58
62
into ((__ , c ) -> both (a , c )))));
59
63
}
60
64
65
+ /**
66
+ * {@inheritDoc}
67
+ */
68
+ @ Override
69
+ public <C > These <A , C > trampolineM (
70
+ Fn1 <? super B , ? extends MonadRec <RecursiveResult <B , C >, These <A , ?>>> fn ) {
71
+ return flatMap (Trampoline .<B , These <A , C >>trampoline (
72
+ b -> sequence (fn .apply (b ).<These <A , RecursiveResult <B , C >>>coerce (),
73
+ RecursiveResult ::terminate )));
74
+ }
75
+
61
76
/**
62
77
* {@inheritDoc}
63
78
*/
@@ -66,6 +81,9 @@ public final <C> These<A, C> pure(C c) {
66
81
return match (a -> both (a , c ), b -> b (c ), into ((a , b ) -> both (a , c )));
67
82
}
68
83
84
+ /**
85
+ * {@inheritDoc}
86
+ */
69
87
@ Override
70
88
@ SuppressWarnings ("unchecked" )
71
89
public <C , App extends Applicative <?, App >, TravB extends Traversable <C , These <A , ?>>,
@@ -80,57 +98,55 @@ AppTrav traverse(Fn1<? super B, ? extends Applicative<C, App>> fn, Fn1<? super T
80
98
* {@inheritDoc}
81
99
*/
82
100
@ Override
83
- @ SuppressWarnings ("unchecked" )
84
101
public final <Z > These <Z , B > biMapL (Fn1 <? super A , ? extends Z > fn ) {
85
- return (These <Z , B >) Bifunctor .super .biMapL (fn );
102
+ return (These <Z , B >) Bifunctor .super .< Z > biMapL (fn );
86
103
}
87
104
88
105
/**
89
106
* {@inheritDoc}
90
107
*/
91
108
@ Override
92
- @ SuppressWarnings ("unchecked" )
93
109
public final <C > These <A , C > biMapR (Fn1 <? super B , ? extends C > fn ) {
94
- return (These <A , C >) Bifunctor .super .biMapR (fn );
110
+ return (These <A , C >) Bifunctor .super .< C > biMapR (fn );
95
111
}
96
112
97
113
/**
98
114
* {@inheritDoc}
99
115
*/
100
116
@ Override
101
117
public final <C > These <A , C > fmap (Fn1 <? super B , ? extends C > fn ) {
102
- return Monad .super .<C >fmap (fn ).coerce ();
118
+ return MonadRec .super .<C >fmap (fn ).coerce ();
103
119
}
104
120
105
121
/**
106
122
* {@inheritDoc}
107
123
*/
108
124
@ Override
109
125
public final <C > These <A , C > zip (Applicative <Fn1 <? super B , ? extends C >, These <A , ?>> appFn ) {
110
- return Monad .super .zip (appFn ).coerce ();
126
+ return MonadRec .super .zip (appFn ).coerce ();
111
127
}
112
128
113
129
@ Override
114
130
public <C > Lazy <These <A , C >> lazyZip (
115
131
Lazy <? extends Applicative <Fn1 <? super B , ? extends C >, These <A , ?>>> lazyAppFn ) {
116
132
return projectA ().<Lazy <These <A , C >>>fmap (a -> lazy (a (a )))
117
- .orElseGet (() -> Monad .super .lazyZip (lazyAppFn ).fmap (Monad <C , These <A , ?>>::coerce ));
133
+ .orElseGet (() -> MonadRec .super .lazyZip (lazyAppFn ).fmap (Monad <C , These <A , ?>>::coerce ));
118
134
}
119
135
120
136
/**
121
137
* {@inheritDoc}
122
138
*/
123
139
@ Override
124
140
public final <C > These <A , C > discardL (Applicative <C , These <A , ?>> appB ) {
125
- return Monad .super .discardL (appB ).coerce ();
141
+ return MonadRec .super .discardL (appB ).coerce ();
126
142
}
127
143
128
144
/**
129
145
* {@inheritDoc}
130
146
*/
131
147
@ Override
132
148
public final <C > These <A , B > discardR (Applicative <C , These <A , ?>> appB ) {
133
- return Monad .super .discardR (appB ).coerce ();
149
+ return MonadRec .super .discardR (appB ).coerce ();
134
150
}
135
151
136
152
/**
0 commit comments