Creating an ES6ish Compose in Javascript
Drew Tipson
6628

Nice insight. You might simplify the expression by just checking for the head (since functions are truthy):

const compose = (head, ...rest) => x => head ? head(compose(...rest)(x)) : x

Same for pipe/flow:

const flow = (head, ...rest) => x => head ? flow(...rest)(head(x)) : x

Simply return x if no functions are given to compose/flow, which reads nicely: “Apply the value to the next function in the list until there’s no next”.

This is also safe when you call compose with no arguments (but I personally don’t like this kind of “safety”).

Like what you read? Give Štefan Maric a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.