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

Partial function application with generics

I'm working with an Observer API (ObserverSet) which have the following function : public func add(object: T, _ f: T -> Parameters -> Void) -> ObserverSetEntry It simply register an object then call the instance method f…
Yaman
  • 3,949
  • 4
  • 38
  • 60
7
votes
1 answer

Currying in Scala: multiple parameter lists vs returning a function

When using the following syntax to define functions with currying enabled: def sum(x: Int)(y: Int)(z: Int) = x + y + z one still has to suffix any calls to curried calls of sum with _: sum _ sum(3) _ sum(3)(2) _ otherwise the compiler will…
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
7
votes
2 answers

Make list of partially applied functions (elegantly or idiomatically)

I can mostly stumble my way through my Haskell questions, but I haven't found a better solution to my problem. Suppose that I have a function f that takes 5 parameters and I want to create a list of partially-applied functions that have the first 3…
7
votes
2 answers

How to pass a code block to function?

I am trying to create a try clause analogue which repeats code block if exception occurred inside this code block. def retry(attempts: Int)(func: Unit => Unit) { var attempt = 0 while (attempt < attempts) { attempt += 1 try { …
tmporaries
  • 1,523
  • 8
  • 25
  • 39
7
votes
1 answer

Equality function for pair components

Is there a function in Scala that compares the two components of a pair for equality? Something like: def pairEquals[A, B](pair: Pair[A, B]): Boolean = (pair._1 == pair._2) In Haskell, that would be: uncurry (==)
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
7
votes
1 answer

Type of a function with Implicit parameters in Scala

I would like to have a higher order function that takes in parameter a function that accepts a specific implicit parameter. To be more precise, I am trying to make a function that takes a Future creation method that depends on an implicit context…
Mortimer
  • 2,966
  • 23
  • 24
7
votes
3 answers

How to map a list of functions over multiple arguments in Haskell?

I have three functions (getRow,getColumn,getBlock) with two arguments (x and y) that each produce a list of the same type. I want to write a fourth function that concatenates their outputs: outputList :: Int -> Int -> [Maybe Int] outputList x y =…
user313967
  • 2,013
  • 2
  • 14
  • 9
7
votes
2 answers

F# parameter passing

I've always thought that F# had two different ways to pass arguments, curry style and tuple style. Is this actually correct? Isn't it simply one style , curry style, and arguments can either be simple values or tuples. e.g. someFunc (a,b) = isn't…
Roger Johansson
  • 22,764
  • 18
  • 97
  • 193
7
votes
2 answers

In F#, how do you curry ParamArray functions (like sprintf)?

In F#, how do you curry a function that accepts a variable number of parameters? I have code like this...(the log function is just an example, the exact implementation doesn't matter) let log (msg : string) = printfn "%s" msg log "Sample" It…
Fendy
  • 73
  • 3
7
votes
2 answers

Scala, Currying on multi parameter-group method including implicit params?

After having discovered that currying multi parameter-groups method is possible, I am trying to get a partially applied function which requires implicit parameters. It seams not possible to do so. If not could you explain me why ? scala> def sum(a:…
kheraud
  • 5,048
  • 7
  • 46
  • 75
6
votes
3 answers

Abusing generics to implement a curried composition function in Java

So, after playing around with Java generics a bit, to get a deeper understanding of their capabilities, I decided to try to implement the curried version of the composition function, familiar to functional programmers. Compose has the type (in…
deontologician
  • 2,764
  • 1
  • 21
  • 33
6
votes
2 answers

When to use currying and partial functions in JavaScript

I read this post on Dr. Dobb's about currying and partial functions in JavaScript. It looks useful, but I'm wondering (as an occasional developer in JavaScript) if there are standard situations where this is regularly used?
conrad carter
  • 266
  • 2
  • 10
6
votes
3 answers

Curried function defined in terms of its own partial application

The following SML code is taken from a homework assignment from a course at University of Washington. (Specifically it is part of the code provided so that students could use it to complete Homework 3 listed on the course webpage.) I am not asking…
lamc
  • 347
  • 2
  • 5
6
votes
2 answers

Can't understand result when calling applyTwice multiple times

There are many questions based on applyTwice, but none that relate to my problem. I understand that the applyTwice function defined like this: applyTwice :: (a -> a) -> a -> a applyTwice f a = f (f a) applies a function twice. So if I have an…
6
votes
1 answer

What is the difference between using Raku's Code.assuming method and using an anonymous Block or Sub?

The Raku docs say that Code.assuming Returns a Callable that implements the same behavior as the original, but has the values passed to .assuming already bound to the corresponding parameters. What is the difference between using .assuming and…
codesections
  • 8,900
  • 16
  • 50