-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from inspect import signature | ||
|
||
def curry(fn): | ||
sig = signature(fn) | ||
return curryN(len(sig.parameters), fn) | ||
|
||
def curryN(n, fn): | ||
return _curryN(n, [], fn) | ||
|
||
def _curryN(n, saved, fn): | ||
def f1(*rest): | ||
newSaved = saved + list(rest) | ||
newSaved = newSaved[:] | ||
if len(newSaved) >= n: | ||
return fn(*newSaved) | ||
else: | ||
return _curryN(n, newSaved, fn) | ||
return f1 | ||
|
||
def add(a, b): | ||
return a + b | ||
|
||
def multiAdd(a, b, c): | ||
return a + b + c | ||
|
||
if __name__ == '__main__': | ||
curryAdd = curry(add) | ||
res = curryAdd(1)(2) | ||
print(res) | ||
|
||
curryMultiAdd = curry(multiAdd) | ||
print(curryMultiAdd(1)(2)(3)) |