Advertisement
tyler569

evil partial pipeline definition thing

Apr 3rd, 2019
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. from functools import reduce, partial
  2.  
  3. def identity(x):
  4.     return x
  5.  
  6. def add3(a, b, c):
  7.     print("adding {}, {}, and {}".format(a, b, c))
  8.     return a + b + c
  9.  
  10. def compose2(f, g):
  11.     # print("composing {} and {}".format(f, g))
  12.     return lambda x: f(g(x))
  13.  
  14. def compose_list(fs):
  15.     return reduce(compose2, fs, identity)
  16.  
  17. def partial_list(fs):
  18.     return partial(*fs)
  19.  
  20.  
  21. pipeline = [
  22.     [ add3, 1, 2 ],
  23.     [ add3, 3, 4 ],
  24.     [ add3, 5, 6 ],
  25. ]
  26.  
  27.  
  28. def main():
  29.     pipe_partials = map(partial_list, reversed(pipeline))
  30.     pipe_partials = list(pipe_partials)
  31.     pipe_composed = compose_list(pipe_partials)
  32.  
  33.     v = pipe_composed(0)
  34.     print(v)
  35.  
  36. if __name__ == "__main__":
  37.     main()
  38.  
  39.  
  40. # $ python3 evil_partials.py
  41. # adding 1, 2, and 0
  42. # adding 3, 4, and 3
  43. # adding 5, 6, and 10
  44. # 21
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement