Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are 'currying' and 'composition' the same concept in Javascript?

Tags:

Recently I read about function composition in a Javascript book, and then on a website I saw someone reference it as currying.

Are they the same concept?

like image 726
Dino Liu Avatar asked Mar 29 '16 01:03

Dino Liu


People also ask

What is composition in JavaScript?

Function composition is an approach where the result of one function is passed on to the next function, which is passed to another until the final function is executed for the final result. Function compositions can be composed of any number of functions.

What is meant by currying in JavaScript?

What is currying in JavaScript? Currying simply means evaluating functions with multiple arguments and decomposing them into a sequence of functions with a single argument.

What is the difference between currying and partial application?

Currying: A function returning another function that might return another function, but every returned function must take only one parameter at a time. Partial application: A function returning another function that might return another function, but each returned function can take several parameters.

What is the difference between closure and currying in JavaScript?

Currying means that the closure does not have to receive all of its arguments at once, but separately. I've found this useful metaphor around the internet: Think of currying as adding ingredients (arguments, or other spices) to a function one by one.


2 Answers

@Omarjmh's answer is good but the compose example is overwhelmingly complex for a learner, in my opinion

Are they the same concept?

No.

First, currying is translating a function that takes multiple arguments into a sequence of functions, each accepting one argument.

// not curried const add = (x,y) => x + y; add(2,3); // => 5  // curried const add = x => y => x + y; add(2)(3); // => 5 

Notice the distinct way in which a curried function is applied, one argument at a time.


Second, function composition is the combination of two functions into one, that when applied, returns the result of the chained functions.

const compose = f => g => x => f(g(x));  compose (x => x * 4) (x => x + 3) (2); // (2 + 3) * 4 // => 20 

The two concepts are closely related as they play well with one another. Generic function composition works with unary functions (functions that take one argument) and curried functions also only accept one argument (per application).

// curried add function const add = x => y => y + x;  // curried multiplication function const mult = x => y => y * x;  // create a composition // notice we only apply 2 of comp's 3 parameters // notice we only apply 1 of mult's 2 parameters // notice we only apply 1 of add's 2 parameters let add10ThenMultiplyBy3 = compose (mult(3)) (add(10));  // apply the composition to 4 add10ThenMultiplyBy3(4); //=> 42  // apply the composition to 5 add10ThenMultiplyBy3(5); //=> 45  
like image 131
Mulan Avatar answered Sep 24 '22 09:09

Mulan


Composition and currying are used to create functions. Composition and currying differ in the way they create new functions (by applying args vs chaining).

Compose:

Compose should return a function that is the composition of a list of functions of arbitrary length. Each function is called on the return value of the function that follows. You can think of compose as moving right to left through its arguments.

Example:

var compose = function(funcs) {   funcs = Array.prototype.slice.call(arguments, 0);   return function(arg) {     return funcs.reduceRight(function (a, b) {       a = a === null ? a = b(arg) : a = b(a);       return a;     }, null);   }; };   var sayHi = function(name){ return 'hi: ' + name;}; var makeLouder = function(statement) { return statement.toUpperCase() + '!';};  var hello = compose(sayHi, makeLouder); l(hello('Johhny')); //=> 'hi: JOHNNY!' 

Currying:

Currying is a way of constructing functions that allows partial application of a function’s arguments.

Example:

var addOne = add(1); var addTwo = add(2);  var addOneToFive = addOne(5); var addTwoToFive = addTwo(5);  l(addOneToFive); //6 l(addTwoToFive); //7 

JSBin with the above examples: https://jsbin.com/jibuje/edit?js,console

like image 22
omarjmh Avatar answered Sep 24 '22 09:09

omarjmh