The Java8 function currying library!
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


The Java8 function currying library!

Q: Can we do currying and partial applcation in Java?

Was a question asked on:

Well, Scala, Clojure or Haskell (or any other functional programming language...) are definitely THE languages to use for currying and other functional tricks.

Having that said is certainly possible to curry with Java without the super amounts of boilerplate one might expect (well, having to be explicit about the types hurts a lot though - just take a look at the curried example ;-)).

The tests bellow showcase both, currying a Function3 into Function1 => Function1 => Function1:

    public void shouldCurryFunction() throws Exception {
      // given
      Function3<Integer, Integer, Integer, Integer> func = (a, b, c) -> a + b + c;

      // when
      Function<Integer, Function<Integer, Function<Integer, Integer>>> cur = curried(func);

      // then
      Function<Integer, Function<Integer, Integer>> step1 = cur.apply(1);
      Function<Integer, Integer> step2 = step1.apply(2);
      Integer result = step2.apply(3);


as well as partial application, although it's not really typesafe in this example:

    public void shouldCurryOneArgument() throws Exception {
      // given
      Function3<Integer, Integer, Integer, Integer> adding = (a, b, c) -> a + b + c;

      // when
      Function2<Integer, Integer, Integer> curried = applyPartial(adding, _, _, put(1));

      // then
      Integer got = curried.apply(0, 0);

This is taken from a Proof Of Concept I've just implemented for fun before JavaOne tomorrow in an hour "because I was bored" ;-) The code is available here:

The general idea could be expanded to FunctionN => FunctionM, relatively easily, though "real typesafety" remains a problem for the partia application example and the currying example would need a hell lot of boilerplaty code in jcurry, but it's doable.

All in all, it's doable, yet in Scala it's out of the box ;-)


Scala is great.


Apache 2.0