I was wondering if there is any way to pull that in Java. I think it is not possible without native support for closures.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Currying a method is always possible in Java, but it does not support it in a standard way. Trying to achieve this is complicated and makes the code pretty unreadable. Java is not the appropriate language for this.
There are a lot of options for Currying with Java 8. Function type Javaslang and jOOλ both offering Currying out of the box (I think this was an oversight in the JDK), and Cyclops Functions module has a set of static methods for Currying JDK Functions and method references. E.g.
'Currying' is also available for Consumers. E.g to return a method with 3 params, and 2 of those already applied we do something similar to this
Javadoc
Yes, see the code example for yourself:
This is simple example with curriedAdd being a curried function which returns another function, and this can be used for partial application of parameters as stored in curried which is a function in itself. This is now later applied fully when we print it on screen.
Moreover, later you can see how you can use it in kind of JS style as
The advantage of using Currying in Java 8 is that it lets you define high order functions and then pass a first order function and function arguments in a chained, elegant way.
Here is an example for Calculus, the derivative function.
While you can do Currying in Java, it is ugly (because its not supported) In Java is it simpler and faster to use plain loops and simple expressions. If you post an example of where you would use currying, we can suggest alternatives which do the same thing.
One can emulate currying with Java 7 MethodHandles: http://www.tutorials.de/threads/java-7-currying-mit-methodhandles.392397/