55import com .jnape .palatable .lambda .functions .Fn3 ;
66import com .jnape .palatable .lambda .functor .Functor ;
77import com .jnape .palatable .lambda .recursionschemes .Algebra ;
8+ import com .jnape .palatable .lambda .recursionschemes .Thunk ;
89import fix .Coalgebra ;
910
11+ import static com .jnape .palatable .lambda .recursionschemes .builtin .Anamorphism .ana ;
12+ import static com .jnape .palatable .lambda .recursionschemes .builtin .Catamorphism .cata ;
13+
1014public final class Hylomorphism <A , B , F extends Functor , FA extends Functor <A , F >, FB extends Functor <B , F >> implements Fn3 <Algebra <FB , B >, Coalgebra <A , FA >, A , B > {
1115
1216 private static final Hylomorphism INSTANCE = new Hylomorphism ();
1317
1418 private Hylomorphism () {
1519 }
1620
17- @ SuppressWarnings ("unchecked" )
21+ @ SuppressWarnings ({ "unchecked" , "Duplicates" } )
1822 public A fapply (Algebra <FA , A > algebra , Coalgebra <A , FA > coalgebra , A a ) {
1923 A x = a ;
2024 FA fa = coalgebra .apply (x );
@@ -28,15 +32,28 @@ public A fapply(Algebra<FA, A> algebra, Coalgebra<A, FA> coalgebra, A a) {
2832 }
2933
3034 return x ;
31- //
32- // return algebra.apply((FB) coalgebra.apply(a).fmap(hylo(algebra, coalgebra)));
33- // return cata(algebra).compose(ana(coalgebra)).apply(a);
3435 }
3536
37+ @ SuppressWarnings ({"unchecked" , "Duplicates" })
38+ public B fapply2 (Algebra <FB , B > algebra , Coalgebra <A , FA > coalgebra , A a ) {
39+ A x = a ;
40+ B currentB = null ;
41+ FA fa = coalgebra .apply (x );
42+ FA previous = null ;
43+ while (fa != previous ) {
44+ previous = fa ;
45+ final FA foo = fa ;
46+ Thunk <B , F > bfThunk = new Thunk <>(() -> foo .fmap (a1 -> fapply2 (algebra , coalgebra , a1 )));
47+ currentB = algebra .apply ((FB ) bfThunk .get ());
48+ }
49+ return currentB ;
50+ }
51+
52+
3653 @ Override
37- @ SuppressWarnings ("unchecked" )
54+ @ SuppressWarnings ({ "unchecked" , "Duplicates" } )
3855 public B apply (Algebra <FB , B > algebra , Coalgebra <A , FA > coalgebra , A a ) {
39- return algebra . apply (( FB ) coalgebra .apply (a ). fmap ( hylo ( algebra , coalgebra )) );
56+ return cata ( algebra ). compose ( ana ( coalgebra )) .apply (a );
4057 }
4158
4259 @ SuppressWarnings ("unchecked" )
0 commit comments