Skip to content

Instantly share code, notes, and snippets.

@kavinyao
Created May 18, 2013 08:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kavinyao/5603699 to your computer and use it in GitHub Desktop.
Save kavinyao/5603699 to your computer and use it in GitHub Desktop.
# This piece of code is a rebound of https://github.com/ForbesLindesay/curry
# Author: Kavin Yao <kavinyao@gmail.com>
# License: MIT
import inspect
def curry(func, num_params=-1):
"""Simple, unlimited curry.
The curried function is not called until desired number of arguments is passed.
Note: **kwarg is not supported
"""
if num_params < 0:
num_params = len(inspect.getargspec(func).args)
def curried(*args):
if len(args) < num_params:
def deep_curried(*more_args):
return curried(*(args+more_args))
return deep_curried
else:
return func(*args)
return curried
if __name__ == '__main__':
sum_four = curry(lambda a, b, c, d: a + b + c + d)
print sum_four(5)(5)(5)(5);# => 20
print sum_four(5, 5)(5, 5);# => 20
print sum_four(5, 5, 5, 5);# => 20
print sum_four(5)(5, 5, 5);# => 20
print sum_four(5, 5, 5)(5);# => 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment