compose() & pipe()

const compose = (...fns) => x => fns.reduceRight((acc, fn) => fn(acc), x)

const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x)

If you want to get real fancy, you can split out and share the reducer.

const apply = (value, fn) => fn(value)

const compose = (...fns) => x => fns.reduceRight(apply, x)

const pipe = (...fns) => x => fns.reduce(apply, x)

Let's talk some more about JavaScript, React, and software engineering.

I write a newsletter to share my thoughts and the projects I'm working on. I would love for you to join the conversation. You can unsubscribe at any time.

Introduction to State Machines and XState Logo
Introduction to State Machines and XState

Check out my courses!

Liked the post? You might like my video courses, too. Click the button to view this course or go to Courses for more information.
View on