JavaScript currying 3 - Compose
Lukas Polak
Posted on September 10, 2020
The compose
function in the code below takes any number of functions, returns a function that takes the initial value, and then uses the reduceRight
function to iterate right-to-left over each function f
in functions
argument and returns the accumulated value y
. In other words, the compose
function creates a pipeline of functions with the output of the function is connected to the input of the next function.
const compose = (...functions) => (x) =>
functions.reduceRight((y, f) => f(y), x);
const g = (n) => n + 1;
const f = (n) => n * 2;
const h = compose(f, g);
h(20); // => 42
💖 💪 🙅 🚩
Lukas Polak
Posted on September 10, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
webdev Understanding HTTP, Cookies, Email Protocols, and DNS: A Guide to Key Internet Technologies
November 30, 2024
softwareengineering Git Mastery: Essential Questions and Answers for Developers 🚀
November 30, 2024