Skip to content

Commit 51646db

Browse files
committed
more wip
1 parent 5bfd1c8 commit 51646db

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

src/main/java/com/jnape/palatable/lambda/recursionschemes/builtin/Anamorphism.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.jnape.palatable.lambda.recursionschemes.Thunk;
88
import fix.Coalgebra;
99

10-
import static com.jnape.palatable.lambda.recursionschemes.builtin.Hylomorphism.hylo;
10+
import static com.jnape.palatable.lambda.recursionschemes.Fix.fix;
1111

1212
public final class Anamorphism<A, F extends Functor, FA extends Functor<A, F>> implements Fn2<Coalgebra<A, FA>, A, Fix<F, Functor<Fix<F, ?>, F>>> {
1313

@@ -18,12 +18,7 @@ private Anamorphism() {
1818

1919
@Override
2020
public Fix<F, Functor<Fix<F, ?>, F>> apply(Coalgebra<A, FA> coalgebra, A a) {
21-
return hylo(f -> () -> new Thunk<>(() -> {
22-
f.fmap(fixed -> coalgebra.apply(fixed.unfix()))
23-
FA apply = coalgebra.apply(a);
24-
return apply;
25-
}).<Fix<F, ?>>fmap(a1 -> ana(coalgebra, a1)).get(), coalgebra, a);
26-
21+
return fix(new Thunk<>(() -> coalgebra.apply(a)).<Fix<F, ?>>fmap(ana(coalgebra)).get());
2722
}
2823

2924
@SuppressWarnings("unchecked")

src/main/java/com/jnape/palatable/lambda/recursionschemes/builtin/Hylomorphism.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
import com.jnape.palatable.lambda.functions.Fn3;
66
import com.jnape.palatable.lambda.functor.Functor;
77
import com.jnape.palatable.lambda.recursionschemes.Algebra;
8+
import com.jnape.palatable.lambda.recursionschemes.Thunk;
89
import 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+
1014
public 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")

src/test/java/com/jnape/palatable/lambda/recursionschemes/builtin/AnamorphismTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@ public void unfoldingToGreatestFixedPoint() {
2525
ana(elements, 0));
2626
}
2727

28-
@Test
28+
// @Test
29+
//todo: wip
2930
public void unfoldingInfiniteCodata() {
30-
Coalgebra<Integer, NatF<Integer>> naturals = i -> {
31-
if (i % 10000 == 0)
32-
System.out.println("i = " + i);
33-
return NatF.s(i + 1);
34-
};
35-
Algebra<NatF<Integer>, Integer> sum = nat -> nat.match(z -> 0, s -> s.carrier() + 1);
36-
Integer x = Hylomorphism.<Integer, Integer, NatF, NatF<Integer>, NatF<Integer>>hylo().fapply(sum, naturals, 0);
31+
Coalgebra<Integer, NatF<Integer>> naturals = i -> NatF.s(i + 1);
32+
Algebra<NatF<String>, String> sum = nat -> nat.match(z -> "0", s -> s.carrier() + 1);
33+
String x = Hylomorphism.<Integer, String, NatF, NatF<Integer>, NatF<String>>hylo().apply(sum, naturals, 0);
3734
System.out.println(x);
3835

3936
}

0 commit comments

Comments
 (0)