44import com .jnape .palatable .lambda .adt .coproduct .CoProduct3 ;
55import com .jnape .palatable .lambda .adt .hlist .Tuple2 ;
66import com .jnape .palatable .lambda .functions .Fn1 ;
7+ import com .jnape .palatable .lambda .functions .recursion .RecursiveResult ;
8+ import com .jnape .palatable .lambda .functions .recursion .Trampoline ;
79import com .jnape .palatable .lambda .functions .specialized .Pure ;
810import com .jnape .palatable .lambda .functor .Applicative ;
911import com .jnape .palatable .lambda .functor .Bifunctor ;
1012import com .jnape .palatable .lambda .functor .builtin .Lazy ;
1113import com .jnape .palatable .lambda .monad .Monad ;
14+ import com .jnape .palatable .lambda .monad .MonadRec ;
1215import com .jnape .palatable .lambda .traversable .Traversable ;
1316
1417import java .util .Objects ;
1518
1619import static com .jnape .palatable .lambda .adt .hlist .HList .tuple ;
1720import static com .jnape .palatable .lambda .functions .builtin .fn1 .Constantly .constantly ;
1821import static com .jnape .palatable .lambda .functions .builtin .fn2 .Into .into ;
22+ import static com .jnape .palatable .lambda .functions .builtin .fn2 .Sequence .sequence ;
1923import static com .jnape .palatable .lambda .functor .builtin .Lazy .lazy ;
2024
2125/**
2832 */
2933public abstract class These <A , B > implements
3034 CoProduct3 <A , B , Tuple2 <A , B >, These <A , B >>,
31- Monad <B , These <A , ?>>,
35+ MonadRec <B , These <A , ?>>,
3236 Bifunctor <A , B , These <?, ?>>,
3337 Traversable <B , These <A , ?>> {
3438
@@ -58,6 +62,17 @@ public final <C> These<A, C> flatMap(Fn1<? super B, ? extends Monad<C, These<A,
5862 into ((__ , c ) -> both (a , c )))));
5963 }
6064
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+
6176 /**
6277 * {@inheritDoc}
6378 */
@@ -66,6 +81,9 @@ public final <C> These<A, C> pure(C c) {
6681 return match (a -> both (a , c ), b -> b (c ), into ((a , b ) -> both (a , c )));
6782 }
6883
84+ /**
85+ * {@inheritDoc}
86+ */
6987 @ Override
7088 @ SuppressWarnings ("unchecked" )
7189 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
8098 * {@inheritDoc}
8199 */
82100 @ Override
83- @ SuppressWarnings ("unchecked" )
84101 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 );
86103 }
87104
88105 /**
89106 * {@inheritDoc}
90107 */
91108 @ Override
92- @ SuppressWarnings ("unchecked" )
93109 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 );
95111 }
96112
97113 /**
98114 * {@inheritDoc}
99115 */
100116 @ Override
101117 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 ();
103119 }
104120
105121 /**
106122 * {@inheritDoc}
107123 */
108124 @ Override
109125 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 ();
111127 }
112128
113129 @ Override
114130 public <C > Lazy <These <A , C >> lazyZip (
115131 Lazy <? extends Applicative <Fn1 <? super B , ? extends C >, These <A , ?>>> lazyAppFn ) {
116132 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 ));
118134 }
119135
120136 /**
121137 * {@inheritDoc}
122138 */
123139 @ Override
124140 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 ();
126142 }
127143
128144 /**
129145 * {@inheritDoc}
130146 */
131147 @ Override
132148 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 ();
134150 }
135151
136152 /**
0 commit comments