Created
November 28, 2012 16:40
-
-
Save katzchang/4162430 to your computer and use it in GitHub Desktop.
HelloCarry.java - @katzchang
This file contains hidden or 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 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