Skip to content

Function Composition in Javascript

Posted on:December 26, 2023 at 02:05 AM

Table of contents

Open Table of contents

Function Composition

Function Composition is the the process of combining two or more functions to produce a new function. It enables you to build complex operations by composing smaller, more focused functions.

Traditional Approach

This is how traditionally we write programs.

const addOne = value => {
  return value + 1;
};

const multiplyByTwo = value => {
  return value * 2;
};

const square = value => {
  return value * value;
};

console.log(square(multiplyByTwo(addOne(5))));
// or
function compose(value) {
  const valAfterAddOne = addOne(value);
  const valAfterMultiplyByTwo = multiplyByTwo(valAfterAddOne);
  const valAfterSquare = square(valAfterMultiplyByTwo);
  return valAfterSquare;
}
compose(5);

Better approach

But there is a better a way to write composed functions. Let’s take an example.

const addOne = value => {
  return value + 1;
};

const multiplyByTwo = value => {
  return value * 2;
};

const square = value => {
  return value * value;
};

function compose(...args) {
  return function (val) {
    return args.reduceRight((acc, fn) => {
      return fn(acc);
    }, val);
  };
}
console.log(compose(square, multiplyByTwo, addOne)(5));

or you can write in ES6 way

// or
const compose =
  (...args) =>
  val =>
    args.reduceRight((acc, fn) => fn(acc), val);
console.log(compose(square, multiplyByTwo, addOne)(5));

As you can see that 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.

Now let’s say you want to add one more function divideBy which takes value and divisor as an argument. how would you add this function inside compose function?

const divide = (val, divisor) => {
  return val / divisor;
};
const compose =
  (...args) =>
  val =>
    args.reduceRight((acc, fn) => fn(acc), val);
console.log(compose(val => divide(val, 2), square, multiplyByTwo, addOne)(5));
// curried function
const divideBy = divisor => val => val / divisor;
const divideBy2 = divideBy(2);

const compose =
  (...args) =>
  val =>
    args.reduceRight((acc, fn) => fn(acc), val);

console.log(compose(divideBy2, square, multiplyByTwo, addOne)(5));

Most of the modern utility libraries provide compose function.

You can also create pipe function as below Just change reduceRight to reduce.

const pipe =
  (...args) =>
  val =>
    args.reduce((acc, fn) => fn(acc), val);