Skip to content

Commit 67e31f2

Browse files
author
stuffacc
committed
Curry uncurry task
1 parent 6e31449 commit 67e31f2

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/6/curry_uncurry.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def curry(func, max_args):
2+
if max_args <= 0:
3+
raise ValueError('max_args must be a positive integer')
4+
5+
def arg(*args):
6+
if len(args) == max_args:
7+
return func(*args)
8+
9+
def new_arg(*new_args):
10+
return arg(*(args + new_args))
11+
12+
return new_arg
13+
14+
return arg
15+
16+
17+
def uncurry(curried_func, max_args):
18+
def uncurry_next(*args):
19+
current_func = curried_func
20+
for arg in args:
21+
current_func = current_func(arg)
22+
23+
return current_func
24+
25+
return uncurry_next
26+
27+
28+
def f4(x, y, z, t):
29+
return x + y + z + t
30+
31+
32+
f_curry = curry(f4, 4)
33+
f_uncurry = uncurry(f_curry, 4)
34+
print(f_curry(3)(4)(5)(6))
35+
print(f_uncurry(3, 4, 5, 6))

0 commit comments

Comments
 (0)