Skip to content

Instantly share code, notes, and snippets.

@garcia
Last active August 29, 2015 13:57
Show Gist options
  • Save garcia/9680770 to your computer and use it in GitHub Desktop.
Save garcia/9680770 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def curry(f, args=None):
def curried(arg):
if args is None:
args_ = []
else:
args_ = args
args_.append(arg)
if len(args_) == f.__code__.co_argcount:
return f(*args_)
else:
return curry(f, args_)
curried.__name__ = f.__name__ + "'" * (len(args) if args else 0)
return curried
def uncurry(f):
def uncurried(arg, *args):
if args:
return uncurry(f(arg))(*args)
else:
return f(arg)
uncurried.__name__ = f.__name__
return uncurried
def main():
@curry
def add3(a, b, c):
return a + b + c
print add3
print add3 (1)
print add3 (1) (2)
print add3 (1) (2) (3)
print
print uncurry(add3)
print uncurry(add3)(1)
print uncurry(add3)(1, 2)
print uncurry(add3)(1, 2, 3)
if __name__ == '__main__':
main()
$ ./curry.py
<function add3 at 0xb7727a74>
<function add3' at 0xb7727e2c>
<function add3'' at 0xb7727df4>
6
<function add3 at 0xb7727e2c>
<function add3' at 0xb7727df4>
<function add3'' at 0xb773b5dc>
6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment