Function Composition

function compose(…funcs) { if (funcs.length === 0) { return arg => arg; } if (funcs.length === 1) { return funcs[0]; } const last = funcs[funcs.length - 1]; const rest = funcs.slice(0, -1); return (…args) => rest.reduceRight((composed, f) => f(composed), last(…args)); } // use function square(num) { return num * num; } function double(num) { return num * 2; } function squareThenDouble(num) { return compose(double, square)(num); } console.log(squareThenDouble(7)); // 98

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.