Last active
August 28, 2022 11:18
-
-
Save RahulDas-dev/cf8aef147ccf3f3b7121a4ab44405769 to your computer and use it in GitHub Desktop.
Pipeline Patterns Implementation Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def make_pipeline2(list_of_callables): | |
| def map_2_partials(item): | |
| func = item[0] | |
| #return func if len(item) < 2 else partial(func, **item[1]) | |
| if len(item) < 2: | |
| return func | |
| else : | |
| return partial(func, **item[1]) | |
| list_of_partials = map(map_2_partials, list_of_callables) | |
| return reduce(lambda f,g: lambda x: g(f(x)), list_of_partials) | |
| def addn(x, n=1): | |
| '''Append n to input string''' | |
| return f'{x}_{n}' | |
| def copyn(x, n=3): | |
| '''copy input string n times''' | |
| return '_'.join([x]*n) | |
| def go_upper(x): | |
| '''convert to upper case''' | |
| return x.upper() | |
| pipeline = make_pipeline2([ (addn,{'n':4}), | |
| (addn,{'n':5}), | |
| (copyn,), | |
| (go_upper,), | |
| ]) | |
| print(pipeline('hi')) | |
| print(make_pipeline2([ (addn,{'n':4}), | |
| (addn,{'n':5}), | |
| (copyn,{'n':2}), | |
| (go_upper,)])('test.01:')) | |
| print(make_pipeline2([(addn,{'n':4}), | |
| (addn,{'n':5}), | |
| (copyn,), | |
| (go_upper,)])('test.02:')) | |
| print(make_pipeline2([(addn,{'n':4}), | |
| (addn,{'n':10}), | |
| (addn,{'n':5}), | |
| (copyn,{'n':2}), | |
| (copyn,{'n':2},)])('test.03:')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment