Created
March 5, 2019 01:10
-
-
Save agumonkey/bfc8d994e93a2ddf487f3107d2d663e5 to your computer and use it in GitHub Desktop.
for when functools.partial is not enough (and you want to reinvent the sauce)
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
# function:defaults code:argcount code:varnames can be used for precise curry | |
def vars(fun): | |
defaults = fun.__defaults__ | |
argcount = fun.__code__.co_argcount | |
varnames = fun.__code__.co_varnames | |
return defaults, argcount, varnames | |
def cu(f): | |
""" | |
TODO: return self.. | |
TODO: handle keywords | |
""" | |
bindings = [] | |
_, _, varnames = vars(f) | |
def satisfied(): | |
return len(varnames) <= len(bindings) | |
def cur(*a,**k): | |
if satisfied(): | |
return f(**dict(bindings)) | |
else: | |
names = varnames[len(bindings):] | |
fresh = list(zip(names, a)) | |
bindings.extend(fresh) | |
if satisfied(): | |
return f(**dict(bindings)) | |
return cur |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment