What I Learned Today 💡 July 2, 2017

Functional Composition: compose() and pipe()

Anton Paras
Jul 5, 2017 · 3 min read

Functions are at the heart of Functional Programming (duh). They are the atomic building block of FP.

As such, a fundamental idea of FP is the “stitching-together” of functions — producing complex functionality by composing small functions that do one thing.

This is function composition. This is achieved by passing the output of one function as the input of another function.

Raw Composition

Consider the following mathematical functions:

You could compose these functions as such:

Here’s a programming analog:

Like in the mathematical example above, you could compose them as:

Here’s a demo:

Image for post
Image for post

Problem

The problem is that composing functions like this doesn’t scale well.
If you have many functions, your composition might end up looking like:

Which is absolutely unsightly.

How do we solve this? compose() and pipe().

compose()

compose() takes functions as input, connects them such that the data flows from RIGHT to LEFT, then returns this combined function.

Image for post
Image for post
The LEFT ← RIGHT flow is for matching the mathematical notation.

Here is an implementation:

Here is an example usage:

You can see that the data flowed LEFT <-- RIGHT.

20 <-- multiplyByFour(5) <-- 5 <-- addTwo(3) <-- 3

This is much better than the raw composition from before. 😊

pipe()

pipe() is nearly identical to compose(). The only difference is that pipe() moves data in the opposite direction: LEFT --> RIGHT.

Image for post
Image for post

Here is an implementation. The only difference is that Array.prototype.reduce is used instead of Array.prototype.reduceRight:

Here is the same example usage. The only difference is that addTwo and multiplyByFour swapped positions. Again, this is because pipe() moves data from LEFT --> RIGHT.

Here is a demo:

Image for post
Image for post

I personally prefer pipe() to compose() because I find LEFT --> RIGHT data-flow more natural. My English-oriented mind naturally reads that way.

Maybe speakers of right-to-left languages and mathematicians prefer compose().

Opinions expressed in these articles do not reflect those of my employer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store