reduce
has an edge case when a list with only one item is supplied: It simply returns the first item. So if you enter [f]
in return reduce(compose1, [f])
it is equal to f
.
If two items are supplied compose1
is actually applied to those two items and returned. So [f, f]
would return:
compose(f, f)
= lambda x: f(f(x))
The next item in the list will be used as second argument together with the previous result as first argument. So if we would supply [f, f, f]
it would return:
compose(compose(f, f), f)
= lambda x: f(f(f(x)))
Now you should see a pattern. Every f
in the list is applied once. So you would need [f] * n
to get a list with n
times f
.