Problem Statement
I would like to apply a list of functions fs = [ f, g, h ] sequentially to a string text=' abCdEf '
Something like f( g( h( text) ) ).
This could easily be accomplished with the following code:
# initial text
text = ' abCDef '
# list of functions to apply sequentially
fs = [str.rstrip, str.lstrip, str.lower]
for f in fs:
text = f(text)
# expected result is 'abcdef' with spaces stripped, and all lowercase
print(text)
Using functools.reduce
It seems that functools.reduce should do the job here, since it "consumes" the list of functions at each iteration.
from functools import reduce
# I know `reduce` requires two arguments, but I don't even know
# which one to chose as text of function from the list
reduce(f(text), fs)
# first interaction should call
y = str.rstrip(' abCDef ') --> ' abCDef'
# next iterations fails, because tries to call ' abCDef'() -- as a function
Unfortunately, this code doesn't work, since each iteration returns a string istead of a function, and fails with TypeError : 'str' object is not callable.

