Skip to content

Instantly share code, notes, and snippets.

@rossdylan
Created February 19, 2014 01:13
Show Gist options
  • Save rossdylan/9084238 to your computer and use it in GitHub Desktop.
Save rossdylan/9084238 to your computer and use it in GitHub Desktop.
better implict function currying in python
from functools import partial
from inspect import getargspec
class Curryable(object):
"""
A decorator that implements implicit function currying in python
What this means is if you call a function with only part of its required
parameters, it will return a function that takes the remaining paramters
"""
def __init__(self, numArgs=-1):
self.numArgs = numArgs
def __call__(self, func):
if self.numArgs == -1:
self.numArgs = len(getargspec(func).args)
if self.numArgs > 0:
@curryable(numArgs=self.numArgs-1)
def wrapper(*args, **kwargs):
if len(args) < self.numArgs:
return partial(func, *args)
else:
return partial(func, *args)(**kwargs)
return wrapper
else:
return func
def curry(func):
return curryable()(func)
@curry
def test_func(one,two,three,four,five):
print(one)
print(two)
print(three)
print(four)
print(five)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment