-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurr.py
22 lines (19 loc) · 943 Bytes
/
curr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def gen_cur(func, minArgs = None):
""" Generates a 'curried' version of a function. """
def g(*myArgs, **myKwArgs):
def f(*args, **kwArgs):
if args or kwArgs: # some more args!
# Allocates data to assign to the next 'f'.
newArgs = myArgs + args
newKwArgs = dict.copy(myKwArgs)
# Adds/updates keyword arguments.
newKwArgs.update(kwArgs)
# Checks whether it's time to evaluate func.
if minArgs is not None and minArgs <= len(newArgs) + len(newKwArgs):
return func(*newArgs, **newKwArgs) # time to evaluate func
else:
return g(*newArgs, **newKwArgs) # returns a new 'f'
else: # the evaluation was forced
return func(*myArgs, **myKwArgs)
return f
return g