Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import reduce, partial
- def identity(x):
- return x
- def add3(a, b, c):
- print("adding {}, {}, and {}".format(a, b, c))
- return a + b + c
- def compose2(f, g):
- # print("composing {} and {}".format(f, g))
- return lambda x: f(g(x))
- def compose_list(fs):
- return reduce(compose2, fs, identity)
- def partial_list(fs):
- return partial(*fs)
- pipeline = [
- [ add3, 1, 2 ],
- [ add3, 3, 4 ],
- [ add3, 5, 6 ],
- ]
- def main():
- pipe_partials = map(partial_list, reversed(pipeline))
- pipe_partials = list(pipe_partials)
- pipe_composed = compose_list(pipe_partials)
- v = pipe_composed(0)
- print(v)
- if __name__ == "__main__":
- main()
- # $ python3 evil_partials.py
- # adding 1, 2, and 0
- # adding 3, 4, and 3
- # adding 5, 6, and 10
- # 21
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement