Skip to content

Instantly share code, notes, and snippets.

@agumonkey agumonkey/cur.py
Created Mar 5, 2019

Embed
What would you like to do?
for when functools.partial is not enough (and you want to reinvent the sauce)
# 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
You can’t perform that action at this time.