jOOQ / jOOL

jOOλ - The Missing Parts in Java 8 jOOλ improves the JDK libraries in areas where the Expert Group's focus was elsewhere. It adds tuple support, function support, and a lot of additional functionality around sequential Streams. The JDK 8's main efforts (default methods, lambdas, and the Stream API) were focused around maintaining backwards compatibility and implementing a functional API for parallelism.
http://www.jooq.org/products
Apache License 2.0
2.08k stars 167 forks source link

Add support for function currying #87

Closed DillonJettCallis closed 9 years ago

DillonJettCallis commented 9 years ago

-Enhancment

Something I've been finding myself doing quite a bit recently is writing extra lambdas for currying my functions. Turning Functions into Suppliers and BiFunctions into Functions by partially applying the first argument. I was writing some static helper methods for simplifying this when I realized that it would be a lot easier and make more sense to just have those methods on the Function interfaces themselves.

So for example, Function2 would have ....

default Function1<T2, R> curry(T1 v1) {
    return v2 -> apply(v1, v2);
}

default Function0<R> curry(T1 v1, T2 v2){
    return () -> apply(v1, v2)
}

While not technically the proper definition of Currying, which would require having only one method returning Function<T1, Function<T2, R>>, I feel like this would produce code that would be easier to read and more familiar to a Java developer. For example if you had a Function4 and wanted to apply the first three arguments, it'd be a simple matter of func.curry(var1, var2, var3) instead of func.curry().apply(var1).apply(var2).apply(var3)

I'm really liking this library and I would be happy to write up the boiler-plate for this and make a pull request. I just wanted to know if there would be any interest in it.

tkroman commented 9 years ago

I've got a couple of places where this would fit, so +1.

lukaseder commented 9 years ago

Sounds reasonable!

danieldietrich commented 9 years ago

Hi!

I'm about to steal that idea for Javaslang, naming it 'partial function application':

// pseudo / no valid code
Function1<T2, R> Function2<T1, T2, R>.apply(T1 t1) = (T2 t2) -> apply(t1, t2);
lukaseder commented 9 years ago

@danieldietrich Stealing the idea and a couple of users at the same time? Sneaky! ;)

danieldietrich commented 9 years ago

Whooops...

A blog post would steal the rest of the users ;-)

Update: jOOL's Java Stream integration is outstanding. I do not have that in my project.

lukaseder commented 9 years ago

Don't worry. I'd still love to publish that blog post. jOOL is not a full-fledged functional API (we don't have time for that). We just fill the gaps where very obvious things are missing from the JDK.

danieldietrich commented 9 years ago

You will get it! I'm currently collecting ideas...

DillonJettCallis commented 9 years ago

@danieldietrich You know, you don't actually have to steal my idea. I can fork javaslang and write it up there too.

danieldietrich commented 9 years ago

@LordBlackhole That would be great but it is already finished. I mentioned you here.

I merge the pull request in a few seconds.

And btw - by hand this would be no fun. There are Function0 to Function26 and Function_i has i - 1 methods for i > 0.

lukaseder commented 9 years ago

@LordBlackhole : I've added also currying using argument tuples to make the API more complete