Skip to content

Instantly share code, notes, and snippets.

@pelotom
Created January 5, 2014 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pelotom/8271550 to your computer and use it in GitHub Desktop.
Save pelotom/8271550 to your computer and use it in GitHub Desktop.
Encoding of an algebraic list type in Java
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