Questions tagged [currying]

Currying is the process of transforming a function of multiple arguments into a function of one argument that returns another function, which takes one fewer argument than the original one. Languages such as Haskell use this as the default argument application mechanism, as it makes certain programming techniques, such as partial application, much easier.

Currying is the process of transforming a function of multiple arguments into a function of one argument that returns another function, which takes one fewer argument than the original one. Languages such as use this as the default argument application mechanism, as it makes certain programming techniques, such as , much easier.

The technique originates in , and was independently discovered by Frege, Schönfinkel and Curry in the early 20th century.

Example of manual currying ()

Uncurried form

/* definition */
let add = (a, b) => a + b;

/* full application */
let x = add(2, 4);

/* partial application */
let add2 = add.bind(null, 2);
let y = add2(4);

Curried form

/* definition */
let add = a => b => a + b;

/* full application */
let x = add(2)(4);

/* partial application */
let add2 = add(2);
let y = add2(4);

Curried programming languages

936 questions
693
votes
21 answers

What is 'Currying'?

I've seen references to curried functions in several articles and blogs but I can't find a good explanation (or at least one that makes sense!)
Ben
  • 9,591
  • 9
  • 32
  • 40
463
votes
15 answers

What is the difference between currying and partial application?

I quite often see on the Internet various complaints that other peoples examples of currying are not currying, but are actually just partial application. I've not found a decent explanation of what partial application is, or how it differs from…
176
votes
16 answers

JavaScript curry: what are the practical applications?

I don’t think I’ve grokked currying yet. I understand what it does, and how to do it. I just can’t think of a situation I would use it. Where are you using currying in JavaScript (or where are the main libraries using it)? DOM manipulation or…
Dave Nolan
  • 2,969
  • 2
  • 27
  • 32
119
votes
6 answers

What does lambda with 2 arrows mean in Java 8?

I have read several Java 8 tutorials before. Right now I encountered following topic: Does java support Currying? Here, I see following code: IntFunction curriedAdd = a -> b -> a +…
gstackoverflow
  • 31,683
  • 83
  • 267
  • 574
99
votes
5 answers

How do I write a function that returns another function?

In Python, I'd like to write a function make_cylinder_volume(r) which returns another function. That returned function should be callable with a parameter h, and return the volume of a cylinder with height h and radius r. I know how to return values…
Julian Das
  • 1,271
  • 2
  • 11
  • 9
94
votes
3 answers

Ordering of parameters to make use of currying

I have twice recently refactored code in order to change the order of parameters because there was too much code where hacks like flip or \x -> foo bar x 42 were happening. When designing a function signature what principles will help me to make the…
92
votes
16 answers

Does Java support Currying?

I was wondering if there is any way to pull that in Java. I think it is not possible without native support for closures.
user855
  • 16,878
  • 34
  • 86
  • 143
83
votes
3 answers

Two ways of currying in Scala; what's the use-case for each?

I am having a discussion around Multiple Parameter Lists in the Scala Style Guide I maintain. I've come to realize that there are two ways of currying, and I'm wondering what the use cases are: def add(a:Int)(b:Int) = {a + b} // Works add(5)(6) //…
davetron5000
  • 21,658
  • 10
  • 64
  • 97
82
votes
4 answers

What's the difference between multiple parameters lists and multiple parameters per list in Scala?

In Scala one can write (curried?) functions like this def curriedFunc(arg1: Int) (arg2: String) = { ... } What is the difference between the above curriedFunc function definition with two parameters lists and functions with multiple parameters in a…
den bardadym
  • 2,520
  • 3
  • 23
  • 27
82
votes
5 answers

Scala currying vs partially applied functions

I realize that there are several questions on here about what currying and partially applied functions are, but I'm asking about how they are different. As a simple example, here is a curried function for finding even numbers: def filter(xs:…
Eric
  • 1,628
  • 2
  • 15
  • 13
78
votes
5 answers

Why does Scala provide both multiple parameters lists and multiple parameters per list?

Multiple parameters lists, e.g. def foo(a:Int)(b:Int) = {} and multiple parameters per list, e.g. def foo(a:Int, b:Int) = {} are semantically equivalent so far as I can tell, and most functional languages have only one way of declaring multiple…
Yuvi Masory
  • 2,584
  • 2
  • 24
  • 34
65
votes
9 answers

Functional programming in C++. Implementing f(a)(b)(c)

I have been getting into the basics of functional programming with C++. I am trying to make a function f(a)(b)(c) that will return a + b + c. I successfully implemented the function f(a)(b) which returns a + b. Here is the code for…
Gigaxel
  • 1,038
  • 8
  • 18
60
votes
3 answers

Haskell function application and currying

I am always interested in learning new languages, a fact that keeps me on my toes and makes me (I believe) a better programmer. My attempts at conquering Haskell come and go - twice so far - and I decided it was time to try again. 3rd time's the…
ttsiodras
  • 7,771
  • 4
  • 43
  • 62
55
votes
2 answers

Usefulness (as in practical applications) of Currying v.s. Partial Application in Scala

I'm trying to understand the advantages of currying over partial applications in Scala. Please consider the following code: def sum(f: Int => Int) = (a: Int, b: Int) => f(a) + f(b) def sum2(f: Int => Int, a: Int, b: Int): Int = f(a) + f(b) …
Hugo Sereno Ferreira
  • 8,665
  • 6
  • 41
  • 88
50
votes
10 answers

How can currying be done in C++?

What is currying? How can currying be done in C++? Please Explain binders in STL container?
yesraaj
  • 42,284
  • 65
  • 185
  • 246
1
2 3
62 63