MartijnPieters answer is excellent. The only thing I would like to add is that this is called a composition of functions.
Naming these generics means you can use them if necessary
from functools import reduce def id(x): return x def comp(f,g): return lambda x: f(g(x)) def compose(*fs): return reduce(comp, fs, id)
Sometimes you want it to look better though -
def seq (x): return lambda k: seq (k (x)) def fn1 (x): return x - 1 def fn2 (x): return x * 3 def fn3 (x): return x + 1 seq (10) (fn1) (fn2) (fn3) (print)
And sometimes you want more flexibility -
from operator import add, mul, sub def seq (x): return lambda k, *v: seq (k (x, *v)) seq (10) (sub, 1) (mul, 3) (add, 1) (print) # 28 # 10 9 27 28 seq (1) (add, 2) (add, 3) (add, 4) (add, 5) (print) # 15 # 1 3 6 10 15 sum = seq(0) for x in range(10): sum = sum (add, x) sum (print) # 45
user633183
source share