Skip to content

Instantly share code, notes, and snippets.

@kofigumbs
Last active June 15, 2017 04:37
Show Gist options
  • Save kofigumbs/0e23d0e0be881a31dfb5c667861956df to your computer and use it in GitHub Desktop.
Save kofigumbs/0e23d0e0be881a31dfb5c667861956df to your computer and use it in GitHub Desktop.
Currying in Java 7
public final class Functio {
abstract static class N1<T, U> {
abstract U apply(T t);
}
abstract static class N2<T, U, V> extends N1<T, N1<U,V>> {
abstract V apply(T t, U u);
@Override
N1<U, V> apply(final T t) {
return new N1<U, V>() {
@Override
V apply(U u) {
return N2.this.apply(t, u);
}
};
}
}
abstract static class N3<T, U, V, Y> extends N1<T, N2<U,V, Y>> {
abstract Y apply(T t, U u, V v);
@Override
N2<U, V, Y> apply(final T t) {
return new N2<U, V, Y>() {
@Override
Y apply(U u, V v) {
return N3.this.apply(t, u, v);
}
};
}
}
public static void main(String[] args) {
Functio.N3<Boolean, Integer, Float, String> function = new Functio.N3<Boolean, Integer, Float, String>() {
@Override
String apply(Boolean b, Integer i, Float f) {
return String.format("%s-%d-%s", b, i, f);
}
};
System.out.println(function.apply(true, 4, 7.5f));
System.out.println(function.apply(true).apply(4, 7.5f));
System.out.println(function.apply(true).apply(4).apply(7.5f));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment