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
0
votes
3 answers

Function.curried not defined

The following Scala code works for me: def curry(s1: String)(s2: String): String = (s1 + " " + s2).toUpperCase val uncurry = Function.uncurried(curry _) println(uncurry("short", "pants")) However the following code does not: def cat(s1: String, s2:…
James Crosswell
  • 648
  • 4
  • 13
0
votes
2 answers

Javascript currying and supporting arbitrary number of invocations

Is there a way to chain a curried function such that you can have an arbitrary number of invocations? The following example only allows you to apply the function twice, the first time calling the curry function, and the second calling the add…
ThinkingInBits
  • 10,792
  • 8
  • 57
  • 82
0
votes
1 answer

Adding Dynamic Parameter to a function with parameter which is passed as parameter to current function

I will walk across the scenario about what i am trying to do: I pass a function with parameter as a parameter to a function. This function will add an additional parameter to the passed function and then make a call to a function. For…
inputError
  • 600
  • 3
  • 12
  • 26
0
votes
2 answers

Bind arguments in different order

Say I have function bake(egg, milk, batter) { ... } And I already have the egg. Then I might do this var f = bake.bind(null, theEgg); to create a function that does the rest of the work. But: what if I have the milk or the batter and I want to…
djechlin
  • 59,258
  • 35
  • 162
  • 290
0
votes
2 answers

Currying in Moose

I'm trying to understand better how the Currying in Moose works. I have used the example in the documentation above, but it doesn't look to work as it is. If I call set_user_agent('MyClient'); I get the following error: Cannot delegate…
barbasa
  • 675
  • 4
  • 22
0
votes
1 answer

Can the F# compiler use currying to separate code paths based on type?

Can the F# compiler separate out code paths by currying a function in which different types imply different paths through subsequent called functions? Consider the following discriminated union. There are 2 possibilities, which are theoretically…
0
votes
1 answer

Scala curried functions appear to not work with JDBC connections

I have an ETL framework I wrote in Scala, and in the name of removing the same try/catch and closing statements for every SQL query and update I perform, I made this trait that I mixin to all my SQL connections. trait SqlConnection { private val…
user2526527
0
votes
3 answers

Currying in scala

w.r.t Currying in scala, partly I understood below sample code. def product1(f:Int => Int )(a:Int, b:Int):Int = { println() if(a > b ) 1 else f(a) * product1(f)(a+1, b) } product(x => x * x) (3, 4) Out of…
BalaB
  • 3,687
  • 9
  • 36
  • 58
0
votes
0 answers

Will this conditional in wu.autoCurry ever be met?

Below is the function. I just don't see how the "else" of that ternary operation could ever get executed, but if I'm missing something I'd like to know wu.autoCurry = function (fn, numArgs) { numArgs = numArgs || fn.length; return function…
edhedges
  • 2,722
  • 2
  • 28
  • 61
0
votes
3 answers

Partially applying a function in C

I have the following code : /* Function that takes a list and applies "function" to every element */ struct list *iter(struct list *l, void (*function)(struct list *a)); And I have a function foo(arg1, arg2, struct list *a that I want to apply to…
EOF
  • 101
0
votes
1 answer

Compile function with curry in groovy

I want to provide some functionality for compiling sources of a specific kind (e.g. java). As the process of compilation needs additional information i need to give in some more arguments and not only the sourcefile. E.g. working directory, compiler…
valenterry
  • 757
  • 6
  • 21
0
votes
1 answer

Can I apply argument defaults when using partial functions in Scala

I have defined two partial functions (hashes), which I expect to take an optional second Boolean parameter: def SHA1 = hash(MessageDigest.getInstance("SHA-1"))_ def MD5 = hash(MessageDigest.getInstance("MD5"))_ private def…
climmunk
  • 1,141
  • 1
  • 12
  • 29
0
votes
1 answer

Function currying - Scheme

We need to write a currying method that does the same like the procedure below. ; Signature: c-bc(n) ; Type: [Number -> [Number -> Number]] ; Purpose: A naive Currying for binomial coefficient (n, k). ; Pre-conditions: n is a natural number ; Tests:…
roh
  • 123
  • 1
  • 1
  • 10
0
votes
2 answers

Haskell Curry Function and Brackets

Hi this is probably a very simple problem but I'm having issue with it. I'm trying to make a roots function with the formula: roots a b c = ((-b + t)/a', (-b - t)/a') where t = b ^ 2 - 4 * a * c a' = 2 * a I'm now trying to make it a curried…
Liamh101
  • 49
  • 1
  • 10
0
votes
1 answer

Function using _.curry does not work as expected

I made a function that checks whether all elements in userRequest array are in whitelist array, using lodash. The implementation is: var isValidRequest = function (whitelist, userRequest) { return _.isArray(userRequest) && _.all(userRequest,…
philipjkim
  • 3,999
  • 7
  • 35
  • 48