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

curry in scheme

I have this curry function: (define curry (lambda (f) (lambda (a) (lambda (b) (f a b))))) I think it's like (define curry (f a b)). my assignment is to write a function consElem2All using curry,which should work like (((consElem2All cons) 'b) '((1)…
bearzk
  • 685
  • 3
  • 7
  • 22
7
votes
3 answers

Reverse currying?

I'd like to compose functions in a certain way. Please consider these 2 functions in pseudocode (not F#) F1 = x + y F2 = F1 * 10 // note I did not specify arguments for F1, 'reverse curry' for lack of a better word What I would like for F# to do is…
gjvdkamp
  • 9,929
  • 3
  • 38
  • 46
7
votes
2 answers

Weird stuff with curried function

I have this weird situation that I don't understand. I'm reading "Programming in Scala" book, Ch. 9. Let's say I have a curried function: def withThis(n:Int)(op:Int=>Unit){ println("Before") op(n); println("After") } When I call…
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272
7
votes
2 answers

Currying Expressions in C#

I am trying to build up an expression tree that I can feed into Linq2SQL so that it will generate a nice clean query. My purpose is to build a filter that takes an arbitrary set of words to AND and NOT (or OR and NOT) together. Because I want to…
sh54
  • 1,230
  • 1
  • 11
  • 20
7
votes
3 answers

Why can't I implicitly cast a Delegate with Extension methods?

I'm trying to figure out a way to automatically cast something to an Action or Func and the best I can come up with is something like this: [TestFixture] public class ExecutionTest { public void BadMethod() { throw new…
user43305
7
votes
5 answers

JS Curry function with Recursion

Kindly read before you mark it as duplicate. Im not asking for single curry call. This functions multiplies, multiplication(4,4,4) //64 function multiplication(...args) { return args.reduce((accum, val) => accum * val, 1) } But Im trying to…
STEEL
  • 8,955
  • 9
  • 67
  • 89
7
votes
1 answer

How do I create a partial function with generics in scala?

I'm trying to write a performance measurements library for Scala. My idea is to transparently 'mark' sections so that the execution time can be collected. Unfortunately I wasn't able to bend the compiler to my will. An admittedly contrived example…
Matteo Caprari
  • 2,869
  • 4
  • 27
  • 35
7
votes
1 answer

Returning a lambda capturing a local variable

Today I encountered a very unintuitive behavior (for me, at least) in C++11 lambdas. The code in question is the following: #include auto sum(int x) { return [&x](int y) { return x + y; }; } int main() { int a =…
Felipe Lopes
  • 186
  • 1
  • 6
7
votes
2 answers

Currying event handlers in React

I am trying to write a (curried?) onChange event handler on a Component that will receive a key argument which will let it know which key in the state object to update. The code won't compile, saying 'key' is not defined. class App extends Component…
7
votes
2 answers

Generic curry function with n-arguments in typescript

I can create a generic currying function for functions with a set number of arguments. IE) function curry2(func:(arg1:T1, arg2:T2) => R, param2: T2):(arg:T1) => R{ return (param1:T1) => func(param1, param2); }; However, I cannot find a…
gnicholas
  • 2,041
  • 1
  • 21
  • 32
7
votes
4 answers

Python currying with any number of variables

I am trying to use currying to make a simple functional add in Python. I found this curry decorator here. def curry(func): def curried(*args, **kwargs): if len(args) + len(kwargs) >= func.__code__.co_argcount: return…
Kevin
  • 977
  • 1
  • 10
  • 17
7
votes
2 answers

Uncurry a curried function of n parameters in javascript

If f :: a -> b -> c is curried then uncurry(f) can be defined as: uncurry :: (a -> b -> c) -> ((a, b) -> c) I'm trying to implement the above function in javascript. Is my below implementation correct and generic enough or are there are any better…
Jyoti Prasad Pal
  • 1,569
  • 3
  • 26
  • 41
7
votes
1 answer

Java 8 partial function application /currying

public static Function partial(BiFunction f, T x) { return (y) -> f.apply(x, y); } In the expression above, I can understand that function partial returns another function, Function. That Function
microwth
  • 1,016
  • 1
  • 14
  • 27
7
votes
4 answers

Need help understanding lambda (currying)

i am reading Accelerated C# i don't really understand the following code: public static Func Bind2nd ( this Func func, TArg2 constant ) { return (x) => func( x, constant…
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
7
votes
3 answers

How to used named parameters with a curried function in scala

I have a method with 4 parameters that gets used in blocks. Within each block, the first parameter is always the same: // Block 1 - first parameter always "A" foo(a="A", b="x", c="y", d="z") foo(a="A", b=".", c=",", d="-") foo(a="A", b="1", c="2",…
rmin
  • 1,018
  • 1
  • 9
  • 18