JSFiddle - React, Tailwind, and code Playground

by manchagnu

JavaScript

function composeWithLambda(...fns) {
	return (value) => fns.reduce((result, fn) => fn(result), value);
}

function composeWithBind(...fns) {
	return fns.reduce.bind(fns, (result, fn) => fn(result));
}

function composeWithLoop(...fns) {
	return function(value) {
  	var result = value;

		for (var index = 0; fns.length > index; index++) {
    	result = fns[index](result);
    }

		return result;
  };
}

function add1(value) {
	return value + 1;
}

function add2(value) {
	return value + 2;
}

function add3(value) {
	return value + 3;
}

const composedAddition = composeWithLoop(add1, add2, add3);

console.log(composedAddition(1));