Created
June 6, 2022 17:51
-
-
Save gabrieldernbach/4bb5942eeea0660ca995b40f2f66492f to your computer and use it in GitHub Desktop.
python method chaining without monads
This file contains 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
import functools | |
from joblib import Parallel, delayed | |
def compose2(f, g): | |
return lambda x: g(f(x)) | |
def compose(*fs): | |
return functools.reduce(compose2, fs) | |
def pipe(x, *fs): | |
return compose(*fs)(x) | |
def broadcast(fun): | |
return lambda xs: (fun(x) for x in xs) | |
def pbroadcast(fun): | |
return lambda xs: Parallel(n_jobs=16)(fun(x) for x in xs) | |
def fork(*fs): | |
return lambda x: (f(x) for f in fs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment