Quantcast
Channel: Functional pipeline using python with advance operators - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Functional pipeline using python with advance operators

$
0
0

I am following the PyData talk in https://youtu.be/R1em4C0oXo8, the presenter whows a library for pipeling call yamal. This library is not open source. So, In my way of learning FP in python, I tried to replicate the basics of that library.

In a nutshell, you build a series of pure functions in python (f1, f2, f3, etc) , and create a list of them as follows:

pipeline = [f1, f2, f3, f4]

Then, you can apply the function run_pipeline, and the result will be the composition:

f4(f3(f2(f1)))

The requirements to the functions are that all have one return value, and except f1, all have one input.

This part is easy to implement I had it done using a composing the functions.

def run_pipeline(pipeline):    get_data, *rest_of_steps = steps    def compose(x):       for f in rest_of_steps:          y = f(x)          x = y       return x    data = get_data()    return compose(data)

The talk show a more advance use of the this abstraction, he defines the "operators"fork and reducer. This "operators" allow to run pipelines as the following:

pipeline1 = [ f1, fork(f2, f3), f4 ]

which is equivalent to: [ f4(f2(f1)), f4(f3(f1)) ]

and

pipeline2 = [ f1, fork(f2, f3), f4, reducer(f5) ]

which is equivalent to f5([f4(f3(f1)), f4(f2(f1))]).

I try to resolve this using functional programming, but I simply can't. I don't know if fork and reducer are decorators (and if so How do I pass the list of following functions?) don't know if I should transform this list to a graph using objects? coroutines? (maybe all of this is nonsense) I simply utterly confused.

Could someone help me about how to frame this using python and functional programming?

NOTE: In the video he talks about observers or executors. for this exercise I don't care about them.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images