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

1062 questions
21
votes
3 answers

Haskell: `Map (a,b) c` versus `Map a (Map b c)`?

Thinking of maps as representations of finite functions, a map of two or more variables can be given either in curried or uncurried form; that is, the types Map (a,b) c and Map a (Map b c) are isomorphic, or something close to it. What practical…
PLL
  • 1,572
  • 1
  • 13
  • 21
20
votes
3 answers

What are real use cases of currying?

I've been reading lots of articles on currying, but almost all of them are misleading, explaining currying as a partial function application and all of examples usually are about functions with arity of 2, like add function or something. Also many…
Roman Liutikov
  • 1,338
  • 10
  • 17
20
votes
2 answers

Why is the function curry called curry?

In many list processing languages (and other languages as well) they have a function called curry, which does some neat things. My question is why do they call it curry? Where does this name come from? My only guess would be the tasty curry dishes…
Teodorico Levoff
  • 1,641
  • 2
  • 26
  • 44
19
votes
6 answers

F# curried function

Anyone have a decent example, preferably practical/useful, they could post demonstrating the concept?
Brian Leahy
  • 34,677
  • 12
  • 45
  • 60
19
votes
3 answers

Scala, currying and overloading

Say you have the following: foo(x: String)(y: Int): Int foo(x: String)(y: Double): Int Scala does not allow such expression. As far as I can see, the reason for this is that foo("asdf") does not have a well defined type (it's either Int => Int or…
Henry Henrinson
  • 5,203
  • 7
  • 44
  • 76
19
votes
4 answers

How do I get (a, b) => c from a => b => c in Scala?

If I have: val f : A => B => C This is shorthand for: val f : Function1[A, Function1[B, C]] How do I get a function g with the signature: val g : (A, B) => C = error("todo") (i.e.) val g : Function2[A, B, C] //or possibly val g : Function1[(A,…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
19
votes
1 answer

What's the difference between currying and multiple parameter lists?

Everywhere I look, I see the terms multiple parameter lists and currying used interchangably. I see it in dozens of stackoverflow questions, and even on scala-lang.org. This page, for example, has the title "Currying". And the first sentence?…
Matt Malone
  • 361
  • 4
  • 25
18
votes
3 answers

Does .Net support curried generics?

Suppose we have a nested generic class: public class A { public class B { } } Here, typeof(A.B<>) is in essence a generic class with two parameters where only the first is bound. If I have a single class with two parameters public…
configurator
  • 40,828
  • 14
  • 81
  • 115
18
votes
5 answers

Why do curried functions require external parameter names?

Given this simple currying function: func foo(x:Int)(y:Int)->String{ return "\(x) with \(y)" } I'd expect to be able to do something like this: let bar = foo(1) bar(2) //<- error: Missing argument label 'y:' in call If I label the call to bar…
jemmons
  • 18,605
  • 8
  • 55
  • 84
17
votes
2 answers

In Python, partial function application (currying) versus explicit function definition

In Python, is it considered better style to: explicitly define useful functions in terms of more general, possibly internal use, functions; or, use partial function application to explicitly describe function currying? I will explain my question…
17
votes
4 answers

When are F# function calls evaluated; lazily or immediately?

Curried functions in F#. I get the bit where passing in a subset of parameters yields a function with presets. I just wondered if passing all of the parameters is any different. For example: let addTwo x y = x + y let incr a = addTwo 1 let added =…
Benboy
  • 353
  • 2
  • 8
17
votes
8 answers

Is it possible to curry method calls in PHP?

I have a SoapClient instance generated for a WSDL file. All except one of the method invocations require the username and the password to be passed id. Is there any way of currying the method calls so that I can omit the username and password?
Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
17
votes
1 answer

How do Haskell currying and pattern matching work together?

I'm new to Haskell. I understand that functions are curried to become functions that take one parameter. What I don't understand is how pattern matching against multiple values can be achieved when this is the case. For example: Suppose we have the…
JamesSwift
  • 863
  • 1
  • 7
  • 16
17
votes
4 answers

Partial application with a C++ lambda?

EDIT: I use curry below, but have been informed this is instead partial application. I've been trying to figure out how one would write a curry function in C++, and i actually figured it out! #include #include template< class…
SplinterOfChaos
  • 469
  • 3
  • 12
16
votes
3 answers

C# Linq vs. Currying

I am playing a little bit with functional programming and the various concepts of it. All this stuff is very interesting. Several times I have read about Currying and what an advantage it has. But I do not get the point with this. The following…
Kai
  • 1,953
  • 2
  • 13
  • 18