Last week: Coin Changer
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 likepipe()
except that the processing order is right to left. Implement it differently frompipe()
(e.g. without reversing the input array) and without callingpipe()
.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 thecallable
type hint. In JavaScript they would be function references of anonymous functions.
In other languages just choose the appropriate things.