DEV Community

Cover image for Curry And Composition Function in JavaScript
Slim Coder
Slim Coder

Posted on

1 2

Curry And Composition Function in JavaScript


In this article i will discuss about:

  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

add(3,c(3)) ;

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️