TDD Kata 08 – Functions Pipeline

This is my weekly Kata post. Read the first one to learn what it is all about.

Last week: Coin Changer

To the Kata description

This week it did not work out for me, I did the kata only once in PHP and that was not enough to get any particular insights. I’ll have to repeat it again some time soon! But on to the next one:

Eighth Kata: Functions Pipeline

The task: build a function pipe() that takes any number of callables as arguments and returns a new callable.
The returned callable passes any arguments to the first callable, then the result of that to the next callable, and so on, and will return the final result.
So the processing order is left to right.

Example:

// first apply strtolower(), then apply ucwords second.
$f = pipe('strtolower', 'ucwords');
$f('FOO BAR') === ucwords(strtolower('FOO BAR'));

Optional follow up exercise:

implement a function compose(), which behaves just like pipe() except that the processing order is right to left. Implement it differently from pipe() (e.g. without reversing the input array) and without calling pipe().

Note: In PHP the callables could be function name strings (e.g. "strtolower"), object callables (e.g. [$instance, "doFoo"]), objects that implement ___invoke(), or \Closure instances (so anything that sattisfied the callable type hint.  In JavaScript they would be function references of anonymous functions.
In other languages just choose the appropriate things.