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
2 answers

How to partially apply member functions in JavaScript?

I currently have a partial-application function which looks like this: Function.prototype.curry = function() { var args = []; for(var i = 0; i < arguments.length; ++i) args.push(arguments[i]); return function() { …
bfops
  • 5,348
  • 5
  • 36
  • 48
6
votes
3 answers

How to specify arg position for functool partial()

As per manual, functools partial() is 'used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature.' What's the best way to specify the positions…
alancalvitti
  • 476
  • 3
  • 14
6
votes
1 answer

When is a => f(a) not equivalent to f?

New to lodash and playing around with it to gain more understanding. I don't understand the behavior of the following code. After learning about the arity argument to _.curry, I have a code snippet that produces results that seems strange to me.…
6
votes
2 answers

How can I have curried case class constructors?

I wrote the following: case class SuperMessage(message: String)(capitalMessage: String = message.capitalize) val message = "hello world" val superMessage = SuperMessage(message)() but I can't do superMessage.capitalMessage What's going on?
JaviOverflow
  • 1,434
  • 2
  • 14
  • 31
6
votes
1 answer

Currying template with parameter from another template

I have class Foo, which has two template parameters, A and B: template struct Foo {}; Also I have class Base, which has one template template parameter: template typename Foo> struct Base {}; I want to…
diralik
  • 6,391
  • 3
  • 28
  • 52
6
votes
4 answers

Lambda as a combination of methods from the Predicate interface doesn't compile if it is written as one statement

What is the difference between both these ways of lambda creation? Why doesn't the first one compile? Predicate predicate = Predicate.isEqual(0).or(Predicate.isEqual(1)); Gives: error: incompatible types: Predicate cannot be…
daniel
  • 107
  • 6
6
votes
4 answers

With TypeScript, can I type a curried version of getProperty

Example from https://www.typescriptlang.org/docs/handbook/advanced-types.html function getProperty(o: T, name: K): T[K] { return o[name]; // o[name] is of type T[K] } Curried version: function curriedGetProperty
chautelly
  • 447
  • 3
  • 14
6
votes
4 answers

qt slots currying

Is there a way curry qt slot? Maybe there is something similar to curryng?
Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
6
votes
1 answer

How to use function currying in Swift 4

I try to understand the function currying tutorial but that code seem out of date. And it's still not really clear about function currying. I try with this function: func curry(_ f: @escaping (A, B) -> C) -> (A) -> (B) -> C { return {…
lee
  • 7,955
  • 8
  • 44
  • 60
6
votes
1 answer

Currying in inversed order in python

Suppose I have a function like this: from toolz.curried import * @curry def foo(x, y): print(x, y) Then I can call: foo(1,2) foo(1)(2) Both return the same as expected. However, I would like to do something like this: @curry.inverse #…
Xiphias
  • 4,468
  • 4
  • 28
  • 51
6
votes
2 answers

Dynamic currying, and how to hold both a function and value in JavaScript variable

I'm learning JavaScript, and I recently came across a practice problem that asked me to construct a function that could create outputs as follows: var threeSum= sum(3); threeSum //3 threeSum(4) //7 threeSum(4)(3) //10 threeSum(4)(3)(7)…
Brad
  • 359
  • 4
  • 12
6
votes
2 answers

F# pipe first parameter

is that possible to pipe the first parameter into a multiple parameter function? for example date = "20160301" Is that possible to pipe date into DateTime.ParseExact( , "yyyyMMDD", CultureInfo.InvariantCulture)
tesla1060
  • 2,621
  • 6
  • 31
  • 43
6
votes
2 answers

Writing a curried javascript function that can be called an arbitrary number of times that returns a value on the very last function call

I'm currently working on a programming problem in my personal time that asks that I make a javascript function that can be called in this manner. add(1) // 1 add(1)(2) // 3 add(1)(2)(3); // 6 add(1)(2)(3)(4); // 10 add(1)(2)(3)(4)(5); // 15 What…
m0meni
  • 16,006
  • 16
  • 82
  • 141
6
votes
1 answer

Currying for functions with n (3 or more) arguments?

For functions with three or more arguments, how does currying work? I searched over SO and Google. Concrete examples given in e.g. What is 'Currying'? ; https://en.wikipedia.org/wiki/Currying are about binary functions f (x, y). In that case, g =…
thor
  • 21,418
  • 31
  • 87
  • 173
6
votes
3 answers

Why can't I make Either instance of Functor using id in Haskell?

When making my custom Either, and Functor, just to understand clearer types and typeclasses, I found the following situation: Functor module Functor (Functor, fmap) where import Prelude hiding(Functor, fmap) class Functor f where fmap :: (a ->…
FtheBuilder
  • 1,410
  • 12
  • 19