Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical use of curried functions?

There are tons of tutorials on how to curry functions, and as many questions here at stackoverflow. However, after reading The Little Schemer, several books, tutorials, blog posts, and stackoverflow threads I still don't know the answer to the simple question: "What's the point of currying?" I do understand how to curry a function, just not the "why?" behind it.

Could someone please explain to me the practical uses of curried functions (outside of languages that only allow one argument per function, where the necessity of using currying is of course quite evident.)

edit: Taking into account some examples from TLS, what's the benefit of

(define (action kind)     (lambda (a b)         (kind a b))) 

as opposed to

(define (action kind a b)     (kind a b)) 

I can only see more code and no added flexibility...

like image 487
Philip Seyfi Avatar asked Feb 03 '11 16:02

Philip Seyfi


People also ask

Can you give an example of a curry function and why this syntax offers an advantage?

const curryOnClick = handler => curryOn (handler) ('click'); Curry functions are neat when used to carry containers of reusable code. Basically you take a function with multiple arguments and you know that one of those arguments will have specific value but the other is undecided.

Why currying is useful in Scala?

Advantages of Currying Function in Scala One benefit is that Scala currying makes creating anonymous functions easier. Scala Currying also makes it easier to pass around a function as a first-class object. You can keep applying parameters when you find them.

What is curr in Python?

Currying is a technique which allows new functions to be created from existing functions by binding one or more parameters to a specific value. It is a major source of function reuse in Python which means that functionality can be written once and then reused in multiple other situations.

Which one of these is an example of higher order function?

Note: Functions such as filter(),map(),reduce(),some() etc, these all are example of Higher-Order Functions.


1 Answers

One effective use of curried functions is decreasing of amount of code.

Consider three functions, two of which are almost identical:

(define (add a b)   (action + a b))  (define (mul a b)   (action * a b))  (define (action kind a b)   (kind a b)) 

If your code invokes add, it in turn calls action with kind +. The same with mul.

You defined these functions like you would do in many imperative popular languages available (some of them have been including lambdas, currying and other features usually found in functional world, because all of it is terribly handy).

All add and sum do, is wrapping the call to action with the appropriate kind. Now, consider curried definitions of these functions:

(define add-curried   ((curry action) +))  (define mul-curried   ((curry action) *)) 

They've become considerable shorter. We just curried the function action by passing it only one argument, the kind, and got the curried function which accepts the rest two arguments.

This approach allows you to write less code, with high level of maintainability.

Just imagine that function action would immediately be rewritten to accept 3 more arguments. Without currying you would have to rewrite your implementations of add and mul:

(define (action kind a b c d e)   (kind a b c d e))  (define (add a b c d e)   (action + a b c d e))  (define (mul a b c d e)   (action * a b c d e)) 

But currying saved you from that nasty and error-prone work; you don't have to rewrite even a symbol in the functions add-curried and mul-curried at all, because the calling function would provide the necessary amount of arguments passed to action.

like image 181
YasirA Avatar answered Oct 21 '22 10:10

YasirA