Given a list of functions (functions) and an integer n, I'm trying to figure out a way to compose them stepwise, and return a list of each stepwise result as follows:
compose_step([lambda x: x+3, lambda x: x+5, lambda x: x+1], 8) --> [8, 11, 16, 17]
So, as of now I have figured out how to compose a list of functions and return the result as follows:
def compose(functions, n):
def compose2(f,g):
return lambda x: f(g(x))
composedFunction = functools.reduce(compose2, functions, lambda x: x)
return composedFunction(n)
However, I'm extremely confused how to keep track of each step and return it as a list. I'm assuming that I need to use map somehow in order to map each stepwise piece to the list. I have also come up with a method to apply all functions in the list to n as such:
def apply_all_functions(functions, n):
answerList = list(map(methodcaller('__call__', n), functions)))
return answerList
I was thinking of somehow using the composeFunction function to compose a new list of stepwise functions, all the way up until the fully composed function, and then using this as my new list for the apply_all_functions in order to achieve the desired result. But currently, I'm pretty stumped.