11package testsupport .recursion ;
22
3+ import com .jnape .palatable .lambda .adt .coproduct .CoProduct2 ;
34import com .jnape .palatable .lambda .functor .Functor ;
45
56import java .util .function .Function ;
67
7- public abstract class ListF <A , B > implements Functor <B , ListF <A , ?>> {
8+ public abstract class ListF <A , B > implements Functor <B , ListF <A , ?>>, CoProduct2 < ListF . Nil < A , B >, ListF . Cons < A , B >> {
89
910 public static <A , B > ListF <A , B > nil () {
1011 return new Nil <>();
1112 }
1213
13- public static <A , B > ListF <A , B > cons (@ SuppressWarnings ("unused" ) A a ,
14- @ SuppressWarnings ("unused" ) B b ) {
15- return new Cons <>();
14+ public static <A , B > ListF <A , B > cons (A head , B tail ) {
15+ return new Cons <>(head , tail );
1616 }
1717
1818 public static final class Nil <A , B > extends ListF <A , B > {
@@ -22,14 +22,42 @@ public static final class Nil<A, B> extends ListF<A, B> {
2222 public <C > ListF <A , C > fmap (Function <? super B , ? extends C > fn ) {
2323 return (Nil <A , C >) this ;
2424 }
25+
26+ @ Override
27+ public <R > R match (Function <? super Nil <A , B >, ? extends R > aFn ,
28+ Function <? super Cons <A , B >, ? extends R > bFn ) {
29+ return aFn .apply (this );
30+ }
2531 }
2632
2733 public static final class Cons <A , B > extends ListF <A , B > {
2834
35+ private final A head ;
36+ private final B tail ;
37+
38+ private Cons (A head , B tail ) {
39+ this .head = head ;
40+ this .tail = tail ;
41+ }
42+
43+ public A head () {
44+ return head ;
45+ }
46+
47+ public B tail () {
48+ return tail ;
49+ }
50+
2951 @ Override
3052 @ SuppressWarnings ("unchecked" )
3153 public <C > ListF <A , C > fmap (Function <? super B , ? extends C > fn ) {
32- return (ListF <A , C >) this ;
54+ return new Cons <>(head , fn .apply (tail ));
55+ }
56+
57+ @ Override
58+ public <R > R match (Function <? super Nil <A , B >, ? extends R > aFn ,
59+ Function <? super Cons <A , B >, ? extends R > bFn ) {
60+ return bFn .apply (this );
3361 }
3462 }
3563}
0 commit comments