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
28
votes
1 answer

Rich Hickey's reason for not auto-currying Clojure functions?

Haskell curries its functions. Clojure does not though it permits partial and function macros as a comparable approach for doing the same. I thought I recalled hearing/reading the reason behind Hickey's decision. Does someone recall what that…
Mario
  • 6,572
  • 3
  • 42
  • 74
28
votes
3 answers

How can go-lang curry?

In functional programming likes Haskell, I can define function add a b = a+b Then add 3 will return a function that take one parameter and will return 3 + something How can I do this in GO? When I define a function that take more than one (say n)…
lazywei
  • 11,955
  • 5
  • 20
  • 25
27
votes
4 answers

How should I make function curry?

In C++14, what is a good way to curry functions or function objects? In particular, I have an overloaded function foo with some random number of overloads: some overloads may be found via ADL, others may be defined in a myriad of places. I have a…
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
26
votes
3 answers

Javascript usages of bind vs curry*?

I'm trying to understand the difference between curry vs bind. The implementation of bind is : /*1*/ Function.prototype.bind = function () /*2*/ { /*3*/ var fn = this, /*4*/ args = Array.prototype.slice.call(arguments); /*5*/ …
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
25
votes
3 answers

Does Haskell have variadic functions/tuples?

The uncurry function only works for functions taking two arguments: uncurry :: (a -> b -> c) -> (a, b) -> c If I want to uncurry functions with an arbitrary number of arguments, I could just write separate functions: uncurry2 f (a, b) = f…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
25
votes
3 answers

Scala Functional Literals with Implicits

Forgive me if this has already been asked elsewhere. I have a Scala syntax question involving function-values and implicit parameters. I'm comfortable using implicits with Scala's currying feature. For instance if I had a sum function and wanted…
shj
  • 1,558
  • 17
  • 23
24
votes
5 answers

Is it possible to "curry" higher-kinded types in Scala?

Let's suppose I have a trait with two type parameters, e.g. trait Qux[A, B] and another trait with a higher-kinded type parameter, e.g. trait Turkle[C[_]] I'd like to be able to substitute a fixed value for one of the type parameters for Qux, so…
Scott Morrison
  • 3,100
  • 24
  • 39
24
votes
2 answers

Currying functions in R

Is it possible to use currying in R? One possibility is to have special paste functions (it can be considered as a follow up to here), e.g. (in incorrect code): '%+%' <- (sep)function(x,y) paste(x,y,sep=sep) "a"%+%("")"b"%+%("_")"c" #gives…
teucer
  • 6,060
  • 2
  • 26
  • 36
24
votes
3 answers

Feed elements of a tuple to a function as arguments in Haskell?

In my Haskell program, I want to use printf to format a list of tuples. I can map printf over a list to print out the values one at a time like this: mapM_ (printf "Value: %d\n") [1,2,3,4] Value: 1 Value: 2 Value: 3 Value: 4 I want to be able to…
davidscolgan
  • 7,508
  • 9
  • 59
  • 78
23
votes
3 answers

Scala: curried constructors

I have the following Scala class: class Person(var name : String, var age : Int, var email : String) I would like to use the Person constructor as a curried function: def mkPerson = (n : String) => (a : Int) => (e : String) => new…
Chris Eidhof
  • 1,514
  • 1
  • 12
  • 15
22
votes
9 answers

Currying decorator in python

I am trying to write a currying decorator in python. I got this far: def curry(fun): cache = [] numargs = fun.func_code.co_argcount def new_fun(*args, **kwargs): print(args) print(kwargs) …
ElfsЯUs
  • 1,188
  • 4
  • 14
  • 34
22
votes
3 answers

Applying an argument list to curried function using foldLeft in Scala

Is it possible to do a foldLeft on a list of arguments, where the initial value supplied to the fold is a fully curried function, the operator is apply, and the list is a list of arguments to be passed to function f? For example, let's say f is…
Adam Pingel
  • 681
  • 4
  • 19
22
votes
6 answers

How does currying work?

I'm very new to Haskell and FP in general. I've read many of the writings that describe what currying is, but I haven't found an explanation to how it actually works. Here is a function: (+) :: a -> (a -> a) If I do (+) 4 7, the function takes 4 and…
Arlen
  • 6,641
  • 4
  • 29
  • 61
22
votes
1 answer

What are the practical advantages of currying?

I see a lot of documentation and questions about what the currying technique is, but I have found very little information on why one would use it in practice. My question is, what are the advantages of currying? Perhaps you can provide a trivial…
MM.
  • 4,224
  • 5
  • 37
  • 74
21
votes
2 answers

Are 'currying' and 'composition' the same concept in Javascript?

Recently I read about function composition in a Javascript book, and then on a website I saw someone reference it as currying. Are they the same concept?
Dino Liu
  • 500
  • 4
  • 17
1 2
3
70 71