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
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,600
  • 7
  • 46
  • 92
52
votes
33 answers

How can I make var a = add(2)(3); //5 work?

I want to make this syntax possible: var a = add(2)(3); //5 based on what I read at http://dmitry.baranovskiy.com/post/31797647 I've got no clue how to make it possible.
rajakvk
  • 9,775
  • 17
  • 46
  • 49
48
votes
19 answers

Variadic curried sum function

I need a js sum function to work like this: sum(1)(2) = 3 sum(1)(2)(3) = 6 sum(1)(2)(3)(4) = 10 etc. I heard it can't be done. But heard that if adding + in front of sum can be done. Like +sum(1)(2)(3)(4). Any ideas of how to do this?
Yaroslav Yakovlev
  • 6,303
  • 6
  • 39
  • 59
48
votes
4 answers

Memoize a curried function

const f = (arg1) => (arg2) => { /* returns something */ } Is it possible to memoize f with regard to the 2 arguments, namely: f(1)(2); f(1)(3); // Cache not hit f(4)(2); // Cache not hit f(1)(2); // Cache hit
jeanpaul62
  • 9,451
  • 13
  • 54
  • 94
45
votes
3 answers

Two ways of defining functions in Scala. What is the difference?

Here is a little Scala session that defines and tries out some functions: scala> def test1(str: String) = str + str; test1: (str: String)java.lang.String scala> test1("ab") res0: java.lang.String = abab works nicely. scala> val test2 =…
aioobe
  • 413,195
  • 112
  • 811
  • 826
44
votes
3 answers

Proper Currying in C#

Given a method DoSomething that takes a (parameterless) function and handles it in some way. Is there a better way to create the "overloads" for functions with parameters than the snippet below? public static TResult…
Rauhotz
  • 7,914
  • 6
  • 40
  • 44
43
votes
0 answers

In functional programming what is "currying"?

Writing as an unreconstructed imperative & OO programmer... Have messed about with Erlang and also Haskell lately. I like Erlang, not sure yet about Haskell. Functional seems more like math than programming, hope that makes sense. Functional…
Eric M
  • 1,027
  • 2
  • 8
  • 21
41
votes
8 answers

How do I define Lisp’s apply in Haskell?

Shouldn’t this definition be allowed in a lazy language like Haskell in which functions are curried? apply f [] = f apply f (x:xs) = apply (f x) xs It’s basically a function that applies the given function to the given list of arguments and is very…
is7s
  • 3,500
  • 1
  • 20
  • 41
41
votes
6 answers

Is there a way to do currying in C?

Say I have a pointer to a function _stack_push(stack* stk, void* el). I want to be able to call curry(_stack_push, my_stack) and get back a function that just takes void* el. I couldn't think of a way to do it, since C doesn't allow runtime function…
user64417
40
votes
3 answers

Haskell Monad bind operator confusion

Okay, so I am not a Haskell programmer, but I am absolutely intrigued by a lot of the ideas behind Haskell and am looking into learning it. But I'm stuck at square one: I can't seem to wrap my head around Monads, which seem to be fairly fundamental.…
Ord
  • 5,693
  • 5
  • 28
  • 42
37
votes
5 answers

FoldLeft using FoldRight in scala

While going through Functional Programming in Scala, I came across this question: Can you right foldLeft in terms of foldRight? How about the other way around? In solution provided by the authors they have provided an implementation as…
sc_ray
  • 7,803
  • 11
  • 63
  • 100
36
votes
3 answers

Higher-order functions in Clojure

Clojure is awesome, we all know this, but that's not the point. I'm wondering what the idiomatic way of creating and managing higher-order functions in a Haskell-like way is. In Clojure I can do the following: (defn sum [a b] (+ a b)) But (sum 1)…
Alfredo Di Napoli
  • 2,281
  • 3
  • 22
  • 28
33
votes
5 answers

Currying with Mathematica

One may implement a limited form of Currying in Mathematica, using this construct: f[a_][b_][c_] := (a^2 + b^2)/c^2 Allowing one to do, for example: f[4][3] /@ Range@5 {25, 25/4, 25/9, 25/16, 1} There is a problem: Attributes only apply to the…
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
29
votes
10 answers

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…
Philip Seyfi
  • 929
  • 1
  • 10
  • 24
29
votes
7 answers

Composing function composition: How does (.).(.) work?

(.) takes two functions that take one value and return a value: (.) :: (b -> c) -> (a -> b) -> a -> c Since (.) takes two arguments, I feel like (.).(.) should be invalid, but it's perfectly fine: (.).(.) :: (b -> c) -> (a -> a1 -> b) -> a -> a1 ->…
Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124
1
2
3
70 71