# Functional Programming in Java: Currying? (for Pierre-Yves)

posted 11 months ago

So, I've been trying to pick up on FP concepts for a while now (or re-pick up, I suppose, I wrote some LISP 30 years ago, but it didn't stick ) One of the things we didn't talk about in that timeframe that seems to come up these days is "Currying". On one hand, I have a vague feeling that I can create the effect fairly easily with a judicious lambda, on the other hand, I'm not sure.

Can you give your thoughts on the relative importance of this concept, and whether you can indicate a good approach for "faking it"? It occurs to me that even if the necessary syntax turns out to be trivial, the illustration might help us grok the concept too

Thanks!

Simon

Can you give your thoughts on the relative importance of this concept, and whether you can indicate a good approach for "faking it"? It occurs to me that even if the necessary syntax turns out to be trivial, the illustration might help us grok the concept too

Thanks!

Simon

posted 11 months ago

Perhaps we should start a "Lambda Curry" thread - or maybe more specifically: "Is a lambda a Curry?".

Because from what I've read on Wiki (although I've never come across the term before), I'd say that's

They are "the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument". And In Java 8, the "tuple" is a stream.

Personally, I like Madras.

Winston

PS: Have a cow for introducing me to a completely new term.

Simon Roberts wrote:One of the things we didn't talk about in that timeframe that seems to come up these days is "Currying". On one hand, I have a vague feeling that I can create the effect fairly easily with a judicious lambda, on the other hand, I'm not sure.

Perhaps we should start a "Lambda Curry" thread - or maybe more specifically: "Is a lambda a Curry?".

Because from what I've read on Wiki (although I've never come across the term before), I'd say that's

*precisely*what it is.

They are "the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument". And In Java 8, the "tuple" is a stream.

Personally, I like Madras.

Winston

PS: Have a cow for introducing me to a completely new term.

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Pierre-Yves Saumont

Author

Ranch Hand

Ranch Hand

Posts: 59

13

posted 11 months ago

In Java, arguments are eagerly evaluated, which means that if a function takes a pair of arguments, they will both be evaluated before the function is applied to them. For example, if you have the following function

both x an y will be evaluated before the function is applied and returns there product.

BTW, this is a function of a tuple of arguments and this tuple is the single argument of the function. The (x, y) notation is the tuple notation in Java but, unfortunately, it can be used only for method and function arguments. (Not for return values!)

In many cases, the two values x an y have very different meaning. For example, if we want to multiply a price to add a tax, we may in fact use only specific predetermined values that does not change during the application life cycle. So we could want to first apply the tax rate once for all and then use the result to apply it to many different prices. This is where currying can help:

Here is the curried form of the above function:

This is a function taking a Double and returning a Function<Double, Double>

Now we can apply several different tax rates to create new functions:

We can now calculate prices using these functions:

Of course, the order of the arguments is important. The argument that change less often (here the tax rate) must come first. There are however techniques to swap arguments of curried functions, as I describe in my book.

There are many use cases for curried functions. For example, many text processing functions will take ta Locale, or an encoding scheme, or both, in addition to the text argument to process. Instead of repeating again and again the arguments that do not change, if is much better to use a curried form of the function and partially apply the non changing arguments.

both x an y will be evaluated before the function is applied and returns there product.

BTW, this is a function of a tuple of arguments and this tuple is the single argument of the function. The (x, y) notation is the tuple notation in Java but, unfortunately, it can be used only for method and function arguments. (Not for return values!)

In many cases, the two values x an y have very different meaning. For example, if we want to multiply a price to add a tax, we may in fact use only specific predetermined values that does not change during the application life cycle. So we could want to first apply the tax rate once for all and then use the result to apply it to many different prices. This is where currying can help:

Here is the curried form of the above function:

This is a function taking a Double and returning a Function<Double, Double>

Now we can apply several different tax rates to create new functions:

We can now calculate prices using these functions:

Of course, the order of the arguments is important. The argument that change less often (here the tax rate) must come first. There are however techniques to swap arguments of curried functions, as I describe in my book.

There are many use cases for curried functions. For example, many text processing functions will take ta Locale, or an encoding scheme, or both, in addition to the text argument to process. Instead of repeating again and again the arguments that do not change, if is much better to use a curried form of the function and partially apply the non changing arguments.