Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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