Sequential function mapping in python

丶灬走出姿态 提交于 2019-12-24 01:57:19

问题


I have a bunch of functions in a list:

funcs = [f1, f2, f3, f4, f5]

and all of the functions take in return a single argument, eg.

f1 = lambda x: x*2

I'd like to map all these functions together

result = lambda x: f5(f4(f3(f2(f1(x)))))

or, iterating over funcs

def dispatch(x):
    for f in funcs:
        x = f(x)
    return x

dispatch works fine, but I couldn't figure out a clean way to do this using iterools. Is it possible? Does this sequential function mapping idiom have a name?


回答1:


There is no point in using itertools here; you are producing one output, and you could not apply this to an infinite iterable. You have to have a finite number of functions in the input iterable for this to work at all.

Use the reduce() function:

from functools import reduce

x = reduce(lambda res, func: func(res), funcs, x)

The functools.reduce() import helps the above work in both Python 2 and 3.

reduce(), together with map(), filter() and, yes, itertools, is an often used tool in functional programming.




回答2:


Another (less efficient, alas) way of looking at Martijn's answer is to realize that you want to compose the list of functions.

# function composition: compose(f,g)(x) = f(g(x))
def compose(f, g):
    return lambda x: f(g(x))

# Identity for function composition
# compose(f, identity)(x) = f(x)
identity = lambda x: x

# result(x) = f1(f2(...fn(x)...))
result = reduce(compose, funcs, identity)


来源:https://stackoverflow.com/questions/35398472/sequential-function-mapping-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!