tyler569 icon

evil partial pipeline definition thing

tyler569 | PRO | 04/04/19 04:15:16 AM UTC | 0 ⭐ | 1247 👁️ | Never ⏰ | []
Python |

810 B

|

None

|

0 👍

/

0 👎

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

Comments