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

What is the differences and possible similarities of closures and currying?

I've read through some of the post on here about closures and currying but I feel like I didn't find the answer. So what's the differences and possibly the similarities of closures and currying? Thanks for the help :)
Nope
  • 34,682
  • 42
  • 94
  • 119
8
votes
1 answer

What are the performance characteristics between curried, partially applied, and 'normal' functions in Scala?

I took a look here: Scala currying vs partially applied functions, but the answers there answer more about the functional and semantic differences between currying, partial application, and normal functions in Scala. I'm interested in learning…
josiah
  • 1,314
  • 1
  • 13
  • 33
8
votes
4 answers

Currying a proc with keyword arguments in Ruby

Say I have a generic Proc, Lambda or method which takes an optional second argument: pow = -> (base, exp: 2) { base**exp } Now I want to curry this function, giving it an exp of 3. cube = pow.curry.call(exp: 3) There's an ambiguity here, arising…
Drenmi
  • 8,492
  • 4
  • 42
  • 51
8
votes
1 answer

Is it possible in F# to curry a middle function argument?

Here's code that works fine: let f x y z = x + y + z let g x y = f x y let h x z = z |> f x So I can write expression "h 1", and FSI displays: val it : (int -> int -> int) = If I call "h 1 2 3", the arguments are applied in the…
Vagif Abilov
  • 9,835
  • 8
  • 55
  • 100
8
votes
1 answer

Method reference with argument

I'm looking for a way to map a tab separated String to an array. Currently, I'm doing it with a lambda expression: stream.map(line -> line.split("\t")); Is there a way to do it with a method reference? I know that stream.map(String::split("\t"))…
mossaab
  • 1,812
  • 4
  • 23
  • 44
8
votes
1 answer

Possible to instance_eval a curried proc?

Suppose I have a class such as this: class Test def test_func 140 end end And a proc, which references a member function from Test: p = ->(x, y) { x + y + test_func } # => # To call p, I bind it to…
John Ledbetter
  • 13,557
  • 1
  • 61
  • 80
8
votes
1 answer

Partial application of curried constructors in Scala

Consider the following: class A(foo: Int)(bar: Int)(baz: Int) object A{ def apply(foo: Int)(bar: Int)(baz: Int) = new A(foo)(bar)(baz) } With the apply method I can do the following: scala> A(1)(2)(3) res12: Script.A = Script$A@7a6229e9 scala>…
Henry Henrinson
  • 5,203
  • 7
  • 44
  • 76
8
votes
2 answers

Haskell notation for composing two functions f and g where g takes multiple arguments

Often I find I want to compose two functions f and g, but g takes multiple arguments. Does Haskell provide a set of operators for that (I know I could write it myself, but it seems fairly common and I don't want to duplicate an operator that…
dspyz
  • 5,280
  • 2
  • 25
  • 63
8
votes
6 answers

How do you curry any javascript function of arbitrary arity?

Let's say I have some function: function g(a,b,c){ return a + b + c } And I'd like to turn it into its "curried" form (in quotations since it's not exactly curried per se): function h(a,b,c){ switch(true){ case (a !== undefined && b…
xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
8
votes
2 answers

Why can't C# compiler infer generic-type delegate from function signature?

I'm working with a function that takes two functions as parameters, and returns a new composed one: public static Action Compose(Action first, Action second) { return new Action(arg => { first(arg); second(); …
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
8
votes
3 answers

What is the difference between these functions

Are these functions exactly same? That is, are 1st and 2nd syntax just convenient shorthand for the last syntax? Or is there some theoretical or practical difference, and if so, what is it? let f1 a b = a + b let f2 a = (fun b -> a + b) let f3 =…
hyde
  • 60,639
  • 21
  • 115
  • 176
8
votes
4 answers

"Uncurrying" an instance method in .NET

Can you create a delegate of an instance method without specifying the instance at creation time? In other words, can you create a "static" delegate that takes as it's first parameter the instance the method should be called on? For example, how…
Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
8
votes
2 answers

How to curry a ... argument by position in R?

This may fall under "you can't, and there's no reason to anyway," but I'm curious if it's possible. At very least, maybe it will be a fun R puzzle. I was pondering currying cat to always append \n. However, cat is written so that it pastes…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
7
votes
1 answer

what are curry and uncurry in high-order functions in ML

fun curry f x y = f (x, y); fun uncurry f (x, y) = f x y; fun compose (f, g) x = f (g x); I understand compose function, but not quite understand curry and uncurry in ML. Can anyone explain these? Also, what do the following two lines mean? (1)…
Jensen
  • 1,653
  • 4
  • 26
  • 42
7
votes
1 answer

(How) Can you curry compose monadic functions?

I have the following functions: f: a -> m[b] g: (b,c) -> m[d] h: (a,c) -> m[d] How can h be expressed as a composition of f and g? Using do/for notation we can implement h easily like so: h: (a,c) => { for { b <- f(a) d <- g(b,c) } yield…
PhD
  • 11,202
  • 14
  • 64
  • 112