Compose

What is compose

Compose is a system design principle that allows us to dynamically comibine serveral funcions and returns the resut.

Example without compose

// data -> fn -> data -> fn -> data

const add1 = (n) => n + 1;
const multiplyBy5 = (n) => n * 5;
const minus10 = (n) => n - 10;

const calculate = (n) => {
  const a = add1(n);
  const b = multiplyBy5(a);
  const c = minus10(b);
  return c;
};

const result = calculate(2);

But we now can do it by composing these three functions, like this:

Example with compose

const composeResult = compose(minus10, multiplyBy5, add1)(2);
const pipeResult = pipe(add1, multiplyBy5, minus10)(2);

Implementation

const composeReducer = (fn1, fn2) => (...args) => fn1(fn2(...args));
const pipeReducer = (fn1, fn2) => (...args) => fn2(fn1(...args));

const compose = (...fns) => fns.reduce(composeReducer);
const pipe = (...fns) => fns.reduce(pipeReducer);

//compose
// (...args) => minus10(multiplyBy5(...args)) --->acc
// (...args) => acc(add1(…args)) --->acc

//pipe
// (...args) => multiplyBy5(add1(...args)) --->acc
// (...args) => minus10(acc(...args)) --->acc

References

A quick introduction to pipe() and compose() in JavaScript

November 19, 2020 - author unknown