Skip to content

Instantly share code, notes, and snippets.

@gabrieldernbach
Created June 6, 2022 17:51
Show Gist options
  • Save gabrieldernbach/4bb5942eeea0660ca995b40f2f66492f to your computer and use it in GitHub Desktop.
Save gabrieldernbach/4bb5942eeea0660ca995b40f2f66492f to your computer and use it in GitHub Desktop.
python method chaining without monads
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