How do I apply a list of functions in a nested way?
Example:
functionList = {f1,f2,f3}
RequiredCommand[functionList,Pi]
such that the required command returns
f3[f2[f1[Pi]]]
or f1[f2[f3[Pi]]] (reversing the order is easy)
I'm searching for an efficient way.
Answer
You'll want Composition[]
or ComposeList[]
for the purpose:
ComposeList[{f1, f2, f3}, x]
{x, f1[x], f2[f1[x]], f3[f2[f1[x]]]}
Composition[f3, f2, f1][x]
f3[f2[f1[x]]]
Since OP wants to be able to feed a list:
(Composition @@ {f3, f2, f1})[x]
f3[f2[f1[x]]]
Comments
Post a Comment