Skip to content

Instantly share code, notes, and snippets.

@katzchang
Created November 28, 2012 16:40
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 katzchang/4162430 to your computer and use it in GitHub Desktop.
Save katzchang/4162430 to your computer and use it in GitHub Desktop.
HelloCarry.java - @katzchang
public class HelloCarry {
static <E1, E2, E3> Func<E1, Func<E2, E3>> carry(final Func2<E1, E2, E3> f2) {
return new Func<E1, Func<E2,E3>>() {
@Override
public Func<E2, E3> apply(final E1 e1) {
return new Func<E2, E3>() {
@Override
public E3 apply(E2 e2) {
return f2.apply(e1, e2);
}};
}};
}
public static void main(String[] args) {
Func2<Long, Long, Long> add = new Func2<Long, Long, Long>() {
@Override
public Long apply(Long e1, Long e2) {
return e1 + e2;
}
};
System.out.println(add.apply(1l, 2l)); // => 3
System.out.println(carry(add).apply(1l).apply(2l)); // => 3
}
}
interface Func<E1, E2> {
E2 apply(E1 e);
}
interface Func2<E1, E2, E3> {
E3 apply(E1 e1, E2 e2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment