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
16
votes
5 answers

Generic curry function with TypeScript 3

TypeScript 3.0 introduced generic rest parameters. Up until this point, curry functions had to be annotated in TypeScript with a finite number of function overloads and a series of conditional statements querying the number of passed arguments…
wagerfield
  • 900
  • 2
  • 8
  • 11
16
votes
5 answers

What is the advantage of Currying in C#? (achieving partial function)

What is the advantage of Currying in C#? What is the advantage of achieving partial function application on a curried function?
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
16
votes
2 answers

Partially applying a function that has an implicit parameter

Can I turn a method which takes an implicit parameter into a function? trait Tx def foo(bar: Any)(implicit tx: Tx) {} foo _ // error: could not find implicit value for parameter tx: Tx I am trying to achieve the following, preferably if I can…
0__
  • 66,707
  • 21
  • 171
  • 266
15
votes
2 answers

How are functions curried?

I understand what the concept of currying is, and know how to use it. These are not my questions, rather I am curious as to how this is actually implemented at some lower level than, say, Haskell code. For example, when (+) 2 4 is curried, is a…
providence
  • 29,135
  • 13
  • 46
  • 62
15
votes
4 answers

Currying out of order in Haskell

Is there an elegant notation for Currying the arguments of a function out of order in Haskell? For example, if you wish to divide 2 by all elements of a list, you can write map ((/) 2) [1,2,3,4,5] However to divide all elements of a list it seems…
hosiers
  • 153
  • 1
  • 4
15
votes
5 answers

How do you curry the 2nd (or 3rd, 4th, ...) parameter in F# or any functional language?

I'm just starting up with F# and see how you can use currying to pre-load the 1st parameter to a function. But how would one do it with the 2nd, 3rd, or whatever other parameter? Would named parameters to make this easier? Are there any other…
Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
15
votes
4 answers

What does uncurry ($) do?

I'm doing some excersises where I have to add a function's type and explain what it does. I'm stuck with this: phy = uncurry ($) The type, according to GHCi is phy :: (a -> b, a) -> b. My haskell knowledge is basic so I really have no idea what it…
user2278354
  • 163
  • 1
  • 6
15
votes
2 answers

Number of arguments and point-free in Haskell

With multiple pattern-matching, different numbers of arguments are impossible, even with point-free! foo True b = b + 2 foo _ = id doesn't work for example. But foo True = (+2) foo _ = id does. Sometimes we can use point-free only in one part of…
L01man
  • 1,521
  • 10
  • 27
15
votes
6 answers

Is it possible to implement auto-currying to the Lisp-family languages?

That is, when you call a function with >1 arity with only one argument, it should, instead of displaying an error, curry that argument and return the resulting function with decreased arity. Is this possible to do using Lisp's macros?
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
14
votes
3 answers

Case classes, pattern matching and curried constructors in Scala

They don't seem to mix that well: abstract class A case class B (var a: Int)(var b: String) extends A case class C extends A The following will not work: B(1)("1") match { case B(a)(b) => print("B") case C() => print("C") } The problem is that…
Henry Henrinson
  • 5,203
  • 7
  • 44
  • 76
14
votes
2 answers

Why is currying and uncurrying not implicit in scala

If I have a function: f : A => B => C I can define an implicit conversion such that this can be used where a function (A, B) => C is expected. This goes in the other direction also. Why are these conversions not implicit (or available implicitly)?…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
14
votes
11 answers

"int -> int -> int" What does this mean in F#?

I wonder what this means in F#. “a function taking an integer, which returns a function which takes an integer and returns an integer.” But I don't understand this well. Can anyone explain this so clear ? [Update]: > let f1 x y = x+y ;; val f1 :…
ali62b
  • 5,363
  • 4
  • 22
  • 25
13
votes
2 answers

Function composition in Haskell with tuple arguments

Sometimes I have two functions of the form: f :: a -> (b1,b2) h :: b1 -> b2 -> c and I need the composition g. I solve this by changing h to h': h' :: (b1,b2) -> c Can you please show me (if possible) a function m, so that: (h . m . f) == (h' .…
Jogusa
  • 5,530
  • 5
  • 24
  • 23
13
votes
2 answers

Does using currying result in lower performance in F#?

When writing a function that can accept currying, you can write it as a single-argument function that returns a function. For example, let add x = let inner y = x + y inner So you can either do: add 3 4 or: let add3 = add 3 add3 4 My…
Cthutu
  • 8,713
  • 7
  • 33
  • 49
13
votes
3 answers

Simple Currying in Ruby

I'm trying to do some currying in ruby: def add(a,b) return a+b end plus = lambda {add} curry_plus = plus.curry plus_two = curry_plus[2] #Line 24 puts plus_two[3] I get the error func_test.rb:24:in `[]': wrong number of arguments (1 for 0)…
MattyW
  • 1,519
  • 3
  • 16
  • 33