Created
October 8, 2019 11:08
-
-
Save L3viathan/2f8d8c653b0adfa756a2ea54cb41b7e8 to your computer and use it in GitHub Desktop.
Composable functions
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 wraps | |
| def composable(f1): | |
| @wraps(f1) | |
| def wrapper(*args, **kwargs): | |
| if not kwargs and len(args) == 1 and callable(args[0]): | |
| f2 = args[0] | |
| @wraps(f2) | |
| def composed(*a, **k): | |
| return f1(f2(*a, **k)) | |
| return composable(composed) | |
| else: | |
| return f1(*args, **kwargs) | |
| return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment