This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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