Created
January 5, 2014 17:55
-
-
Save pelotom/8271550 to your computer and use it in GitHub Desktop.
Encoding of an algebraic list type in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static abstract class List<a> { | |
private static final class $Nil<a> extends List<a> { | |
private $Nil() { | |
} | |
public <M> M match(final M $ifNil, final $F<a, $F<List<a>, M>> $ifCons) { | |
return $ifNil; | |
} | |
} | |
private static final class $Cons<a> extends List<a> { | |
private final a $1; | |
private final List<a> $2; | |
private $Cons(final a $1, final List<a> $2) { | |
this.$1 = $1; | |
this.$2 = $2; | |
} | |
public <M> M match(final M $ifNil, final $F<a, $F<List<a>, M>> $ifCons) { | |
return $ifCons.$apply($1).$apply($2); | |
} | |
} | |
public abstract <M> M match(final M $ifNil, final $F<a, $F<List<a>, M>> $ifCons); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment