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

Scala Currying: Overriding function with an empty argument by a partial function

I'm trying to implement/override a function with an empty input argument by using partial function. It is best explained by this non-working minimal example: trait T trait TFactory { def build(): T } class A(someParameter: Int) extends T object…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
0
votes
1 answer

Understanding call by name with currying in Scala

I am trying to understand database connections in Scala using the default Anorm library in play framework. Play has a sample example "Computer Database" where one of the functions tries to retrieve a computer from the DB: DB.withConnection {…
Richeek
  • 2,068
  • 2
  • 29
  • 37
0
votes
2 answers

SML constantly implementation

I read about function constantly: fun constantly k a = k But I don't understand how to work with it. I tried that way: val a = constantly 10; stdIn:32.5-32.28 Warning: type vars not generalized because of value restriction are instantiated to…
senior_pimiento
  • 702
  • 1
  • 5
  • 15
0
votes
1 answer

How do I curry this function in Scala?

So I according to what I've read I've gotten this form of currying down: def arithmetic_iter(op: (Int, Int) => Int, f: Int => Int, base: Int)(a: Int, b: Int): Int = { def iter(a: Int, base: Int): Int = if (a > b) base else iter(a +…
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95
0
votes
0 answers

Explanation of currying in simple terms

I'm currently learning about Haskell and have come across something called currying and curried values. I have also found that (+) is a curried version of + + :: (Integer, Integer) -> Integer (+) :: Integer -> Integer -> Integer From what I can…
orange
  • 5,297
  • 12
  • 50
  • 71
0
votes
1 answer

Scala: Typed Method that returns a subType

I'm trying to write a function that returns a partially applied function which returns a subType of a particular abstract class. I have an abstract class abstract class IsoBoxReader I have a derived class class FileTypeBoxReader( val box, val…
Kartik Aiyer
  • 586
  • 1
  • 6
  • 20
0
votes
1 answer

uncurry and curry functions

I´m quite new to lambda-calculus and I´m trying to do the following exercise, but I´m not able to resolve it. uncurry(curry E) = E Could anyone help me?
ikerexxe
  • 39
  • 5
0
votes
1 answer

Handling anonymous functions in SML datatypes

I have the following datatype and 3 examples of tests: datatype 'a test = Test of ('a -> bool) * string; val pos = Test (fn x => x > 0, "pos"); val even = Test (fn x => x mod 2 = 0, "even"); val small = Test (fn x => x < 100, "small"); I'm still…
user595334
0
votes
1 answer

Using ListPair.foldr to implement zipWith in SML

Background: Beginner level at SML My assignment requires me to use ListPair.foldr and only this function to implement the zipWith function. ListPair.foldr : ('a * 'b * 'c -> 'c) -> 'c -> 'a list * 'b list -> 'c zipWith : ('a * 'b -> 'c) -> 'a list…
user1981428
0
votes
3 answers

Haskell Beginner: Currying/List Associativity

From Learn You a Haskell: Think about this list: [5]. That’s just syntactic sugar for 5:[]. On the left side of the :, there’s a value; on the right side, there’s a list. In this case, it’s an empty list. Now how about the list [4,5]? Well,…
0
votes
1 answer

Uppercase first letter, naming convention for curryed definitions?

In Play2 documentation, I find things like this: def LoggingAction(f: Request[AnyContent] => Result): Action[AnyContent] = { Action { request => Logger.info("Calling action") f(request) } } Is it a convention in Scala to use an…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
0
votes
2 answers

Partial application of operators

If I want to add a space at the end of a character to return a list, how would I accomplish this with partial application if I am passing no arguments? Also would the type be? space :: Char -> [Char] I'm having trouble adding a space at the end due…
user1670032
  • 750
  • 3
  • 12
  • 27
-1
votes
1 answer

Curried class constructor with more than two levels of currying

I have been using currying with scala for literally years, but recently had reason to write a class constructor that is sort of a builder, where I don't want c and d to be optionals, so I don't want the class to be instantiated until they are…
-1
votes
2 answers

Partial application of Printf.ksprintf

I'm trying to write a version of Printf.printf that always appends a newline character after writing its formatted output. My first attempt was # let say fmt = Printf.ksprintf print_endline fmt;; val say : ('a, unit, string, unit) format4 -> 'a =…
mndrix
  • 3,131
  • 1
  • 30
  • 23
-1
votes
1 answer

JavaScript Bind, Apply, and the `this` pointer

I got a little confused with JS bind, apply, and this. Questions Why are this and null interchangeable in the following snippet? Does this in the following context point to the window? function curry(fn) { // your code here return function…
bbcts
  • 25
  • 3