DEV Community is a community of 786,923 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. Curry And Composition Function in JavaScript

1. Higher Order Function

2. Curry Function

3. Composite Function

1) Higher Order Function

Higher-order functions are regular functions that do one or both of the following:

1. Takes one or many functions as arguments

2. Returns a function

Let’s look at simple example.

const c = b => b ;

add(a,b) => a+b; // here b is a function

2) Function Curry

A curried function is a function which takes multiple parameters one at a time, by taking the first argument, and returning a series of functions which each take the next argument until all the parameters have been fixed, and the function application can complete, at which point, the resulting value is returned.

const add = a => b => a + b;

const result = add(2)(3); // 5

3) Composite Function

Curried functions are particularly useful in the context of function composition.

In terms of algebra :

g: a -> b
f: b -> c

Suppose :

h: a -> c
h = f . g = f(g(x))

// Algebra definition, borrowing the . composition operator

In JavaScript :

const g = n => n + 1;

const f = n => n * 2;

const h = x => f(g(x));

h(2); // 6