Last active
August 14, 2017 19:22
-
-
Save dhilst/1f45b4a8273142b8a262805dcfe4b46b to your computer and use it in GitHub Desktop.
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
| from functools import partial, reduce | |
| from inspect import signature, Parameter | |
| import operator as o | |
| def compose(*funcs): | |
| def compose2(f, g): | |
| return lambda *args, **kwargs: f(g(*args, **kwargs)) | |
| return reduce(compose2, funcs) | |
| def curry(f): | |
| def _(arg): | |
| try: | |
| return f(arg) | |
| except TypeError: | |
| return curry(partial(f, arg)) | |
| return _ | |
| inc = partial(o.add, 1) | |
| double = partial(o.mul, 2) | |
| inc_double = compose(double, inc) | |
| print(inc_double(2)) | |
| @curry | |
| def add(a,b): | |
| return a + b | |
| @curry | |
| def trimul(a,b,c): | |
| return a * b * c | |
| print(add(1)(2)) | |
| print(trimul(2)(2)(2)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment