Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit51646db

Browse files
committed
more wip
1 parent5bfd1c8 commit51646db

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
importcom.jnape.palatable.lambda.recursionschemes.Thunk;
88
importfix.Coalgebra;
99

10-
importstaticcom.jnape.palatable.lambda.recursionschemes.builtin.Hylomorphism.hylo;
10+
importstaticcom.jnape.palatable.lambda.recursionschemes.Fix.fix;
1111

1212
publicfinalclassAnamorphism<A,FextendsFunctor,FAextendsFunctor<A,F>>implementsFn2<Coalgebra<A,FA>,A,Fix<F,Functor<Fix<F, ?>,F>>> {
1313

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

1919
@Override
2020
publicFix<F,Functor<Fix<F, ?>,F>>apply(Coalgebra<A,FA>coalgebra,Aa) {
21-
returnhylo(f -> () ->newThunk<>(() -> {
22-
f.fmap(fixed ->coalgebra.apply(fixed.unfix()))
23-
FAapply =coalgebra.apply(a);
24-
returnapply;
25-
}).<Fix<F, ?>>fmap(a1 ->ana(coalgebra,a1)).get(),coalgebra,a);
26-
21+
returnfix(newThunk<>(() ->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
importcom.jnape.palatable.lambda.functions.Fn3;
66
importcom.jnape.palatable.lambda.functor.Functor;
77
importcom.jnape.palatable.lambda.recursionschemes.Algebra;
8+
importcom.jnape.palatable.lambda.recursionschemes.Thunk;
89
importfix.Coalgebra;
910

11+
importstaticcom.jnape.palatable.lambda.recursionschemes.builtin.Anamorphism.ana;
12+
importstaticcom.jnape.palatable.lambda.recursionschemes.builtin.Catamorphism.cata;
13+
1014
publicfinalclassHylomorphism<A,B,FextendsFunctor,FAextendsFunctor<A,F>,FBextendsFunctor<B,F>>implementsFn3<Algebra<FB,B>,Coalgebra<A,FA>,A,B> {
1115

1216
privatestaticfinalHylomorphismINSTANCE =newHylomorphism();
1317

1418
privateHylomorphism() {
1519
}
1620

17-
@SuppressWarnings("unchecked")
21+
@SuppressWarnings({"unchecked","Duplicates"})
1822
publicAfapply(Algebra<FA,A>algebra,Coalgebra<A,FA>coalgebra,Aa) {
1923
Ax =a;
2024
FAfa =coalgebra.apply(x);
@@ -28,15 +32,28 @@ public A fapply(Algebra<FA, A> algebra, Coalgebra<A, FA> coalgebra, A a) {
2832
}
2933

3034
returnx;
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+
publicBfapply2(Algebra<FB,B>algebra,Coalgebra<A,FA>coalgebra,Aa) {
39+
Ax =a;
40+
BcurrentB =null;
41+
FAfa =coalgebra.apply(x);
42+
FAprevious =null;
43+
while (fa !=previous) {
44+
previous =fa;
45+
finalFAfoo =fa;
46+
Thunk<B,F>bfThunk =newThunk<>(() ->foo.fmap(a1 ->fapply2(algebra,coalgebra,a1)));
47+
currentB =algebra.apply((FB)bfThunk.get());
48+
}
49+
returncurrentB;
50+
}
51+
52+
3653
@Override
37-
@SuppressWarnings("unchecked")
54+
@SuppressWarnings({"unchecked","Duplicates"})
3855
publicBapply(Algebra<FB,B>algebra,Coalgebra<A,FA>coalgebra,Aa) {
39-
returnalgebra.apply((FB)coalgebra.apply(a).fmap(hylo(algebra,coalgebra)));
56+
returncata(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
publicvoidunfoldingInfiniteCodata() {
30-
Coalgebra<Integer,NatF<Integer>>naturals =i -> {
31-
if (i %10000 ==0)
32-
System.out.println("i = " +i);
33-
returnNatF.s(i +1);
34-
};
35-
Algebra<NatF<Integer>,Integer>sum =nat ->nat.match(z ->0,s ->s.carrier() +1);
36-
Integerx =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+
Stringx =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)

[8]ページ先頭

©2009-2025 Movatter.jp