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

how to partially apply arbitrary argument of a function?

I want to use partial from functools to partially apply a function's second argument, I know it is easy to do with lambda rather than partial as follows >>> def func1(a,b): ... return a/b ... >>> func2 = lambda x:func1(x,2) >>> func2(4) 2 but I…
Kavin Eswaramoorthy
  • 1,595
  • 11
  • 19
6
votes
1 answer

Successive function application in MATLAB

How do I do successive function application in MATLAB with anonymous functions? Something like the following: g = @(x) @(y) x+y; g(1)(2) However MATLAB gives an error at line 2: ()-indexing must appear last in an index expression. But the following…
6
votes
4 answers

In Scala invoking no-parameter function with and without brackets is executed in different way

I have following Currying function declaration: def logString(count: Int)(fun:() => Unit) { for (n <- 1 to count) { fun } } I call this function in this way: logString(3) { () => print("I") } The result is nothing - just no output. Then I just…
myQs
  • 91
  • 5
6
votes
3 answers

How to disambiguate case class creation with multiple parameter lists?

I have a case class that looks about like this: case class A(first: B*)(second: C*) Both first and second are repeated, so I put the in separate parameter lists. However, I expect that second might be empty in a substantial number of cases, so…
Silly Freak
  • 4,061
  • 1
  • 36
  • 58
6
votes
3 answers

Challenge: Neater way of currying or partially applying C#4's string.Join

Background I recently read that .NET 4's System.String class has a new overload of the Join method. This new overload takes a separator, and an IEnumerable which allows arbitrary collections to be joined into a single string without the need to…
Damian Powell
  • 8,655
  • 7
  • 48
  • 58
6
votes
1 answer

When can I use the Whatever star?

Following this post on perlgeek, it gives an example of currying: my &add_two := * + 2; say add_two(5); # 7 Makes sense. But if I swap the + infix operator for the min infix operator: my &min_two := * min 2; say min_two(5); # Type check failed in…
Phil H
  • 19,928
  • 7
  • 68
  • 105
6
votes
0 answers

Ambiguous type variable in polyvariadic curry definition

So, I'm trying to implement a polyvariadic ZipWithN as described here. Unfortunately, Paczesiowa's code seems to have been compiled with outdated versions of both ghc and HList, so in the process of trying to understand how it works, I've also…
Lily
  • 155
  • 1
  • 6
6
votes
2 answers

How does curry (==) work?

I understand that: (==) :: Eq a => a -> a -> Bool An example of application may be (==) 2 2, which result is True. and that: uncurry (==) :: Eq b => (b, b) -> Bool. An example of application may be uncurry (==) (2, 2), which result is True. But…
Fof
  • 407
  • 2
  • 8
6
votes
2 answers

Curried function in scala

I have a definition of next methods: def add1(x: Int, y: Int) = x + y def add2(x: Int)(y: Int) = x + y the second one is curried version of first one. Then if I want to partially apply second function I have to write val res2 = add2(2) _.…
maks
  • 5,911
  • 17
  • 79
  • 123
6
votes
1 answer

Boost Lambda/Phoenix - how to do lambda which returns another lambda?

Does Boost Lambda/Phoenix supports out of box something like lambda which returns another lambda? For instance, that can be used to do some kind of currying: std::cout << [](int x){return [=](int y){return x+y;};}(1)(2); How to achieve similar…
qble
  • 1,256
  • 2
  • 12
  • 29
6
votes
2 answers

Currying and multiple integrals

I am interested in learning an elegant way to use currying in a functional programming language to numerically evaluate multiple integrals. My language of choice is F#. If I want to integrate f(x,y,z)=8xyz on the region [0,1]x[0,1]x[0,1] I start by…
6
votes
2 answers

Is there a way to get a Curried form of the binary operators in SML/NJ?

For example, instead of - op =; val it = fn : ''a * ''a -> bool I would rather have - op =; val it = fn : ''a -> ''a -> bool for use in val x = getX() val l = getList() val l' = if List.exists ((op =) x) l then l else x::l Obviously I can do this…
Andrew Keeton
  • 22,195
  • 6
  • 45
  • 72
6
votes
2 answers

Implementing a higher order function that performs currying in scala

A coworker of mine sent me a question as follows: Implement a HOF(higher order function) that performs currying, the signature of your function is as follows: def curry[A,B,C](f:(A,B) => C) : A => B => C Similarly, implement a function that…
sc_ray
  • 7,803
  • 11
  • 63
  • 100
6
votes
3 answers

Why can't scala infer the type of the omitted parameters in partial application?

consider this : scala> def sum(x:Int,y:Int) = x+y sum: (x: Int, y: Int)Int scala> sum(1,_:String) :9: error: type mismatch; found : String required: Int sum(1,_:String) Apparently Scala is very well aware of the exact…
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68
6
votes
3 answers

Was point free functions able to inline?

let inline myfunction x y = ... let inline mycurried = myfunction x // error, only functions may be marked inline It seems impossible to explicitly inline curried functions. So whenever mycurried is called, it won't get inlined even if myfunction…
colinfang
  • 20,909
  • 19
  • 90
  • 173