python function pipeline

Posted by Jeffery Yee | 10:04 PM

Python Function Pipeline

In this article, we show how to use "Map & Reduce, not loop" to implement function pipeline in python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def even_filter(nums):
    return filter(lambda x: x%2==0, nums)
 
def multiply_by_three(nums):
    return map(lambda x: x*3, nums)
 
def convert_to_string(nums):
    return map(lambda x: 'The Number: %s' % x,  nums)
 
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
pipeline = convert_to_string(
               multiply_by_three(
                   even_filter(nums)
               )
            )
for num in pipeline:
    print num
In the above code, you have to use nested functions to achieve the goal, which is kinda clunky. Actually, a more natural way is as following. 
1
2
3
pipeline_func(nums, [even_filter,
                     multiply_by_three,
                     convert_to_string])
Here is how we implents pipeline_func: 
1
2
3
4
def pipeline_func(data, fns):
    return reduce(lambda a, x: x(a),
                  fns,
                  data)

0 comments

Popular Posts

无觅相关文章插件,迅速提升网站流量