#Higher-Order Functions#Functional

Compose Functions Right to Left

Fold an array of functions into one, applied right to left.

By Pratik RaiEasy

Write compose(functions) that folds the array into one function. Calling the result with a value feeds that value to the last function first, then passes each output leftwards to the function before it.

So compose([a, b, c])(v) means a(b(c(v))). With no functions at all, the result simply hands back whatever it was called with.

Examples

Input:

JSfile.javascript
1const fn = compose([(x) => x * 10, (x) => x - 1]); 2fn(5);

Output:

40

The rightmost function runs first: 5 minus 1 is 4, and 4 times 10 is 40. Running them left to right would have given 49.

Constraints

  • Each function takes one value and returns one value
  • The array may be empty

Notes

  • Reverse the direction and you have pipe, which many codebases prefer because it reads in execution order.

How to solve it

Right to left

compose([f, g, h])(x) is f(g(h(x))) — the last function in the array sees the input first. That ordering comes from mathematics, where f ∘ g means apply g then f.

Approach

It is a fold from the right, starting at the input value.

Implementation

JSfile.javascript
1function compose(functions) { 2 return (x) => functions.reduceRight((acc, fn) => fn(acc), x); 3}

Worth knowing

The empty case falls out for free: with no functions, reduceRight returns the initial value, which is the input unchanged.

Reverse the direction and you have pipe, which many codebases prefer precisely because it reads in execution order. Knowing why both exist is usually the follow-up.

Goal: Return one function that applies the whole array right to left.

Frequently asked questions

What is function composition?
Combining several functions into one, where the output of each becomes the input of the next. compose([f, g])(x) is f(g(x)).
Why does compose run right to left?
It follows mathematical notation, where f ∘ g means apply g first. Reversing the direction gives pipe, which many codebases prefer because it reads in the order things happen.
What should compose return for an empty array?
The identity function — one that returns its input unchanged. Using reduceRight with the input as the initial value gives that without a special case.
Where is composition used in real code?
Redux middleware and applyMiddleware are built on it, as are most functional utility libraries. It also underpins how a chain of transformations is expressed without nesting calls twenty levels deep.

Related Challenges

Continue learning with these related challenges

View All
JavaScript

Run a Function Only Once

A once wrapper: the first call runs, every call after it does nothing.

JavaScript · ES6 — Pratik Rai ·

JavaScript

Promisify a Callback Function

Convert a Node-style (error, value) callback API into one that returns a promise.

JavaScript · ES6 — Pratik Rai ·

JavaScript

Memoize a Function

Cache a function by its arguments so the same call never computes twice.

JavaScript · ES6 — Pratik Rai ·