Skip to content

Commit a6c162a

Browse files
committed
Implement uncurrying
1 parent 0510457 commit a6c162a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

currying/src/lib.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@ def inner(*args):
1818
return lambda arg: inner(*args, arg)
1919

2020
return lambda arg: inner(arg)
21+
22+
23+
# Uncurry a curried function
24+
def uncurry(f: callable):
25+
def inner(*args):
26+
value = f
27+
28+
for arg in args:
29+
try:
30+
value = value(arg)
31+
except Exception:
32+
raise Exception("incorrect amount of arguments provided")
33+
34+
return value
35+
36+
return inner

currying/tests/test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from ..src.lib import curry
3+
from ..src.lib import curry, uncurry
44

55

66
def add_args(*args):
@@ -33,3 +33,13 @@ def test_unspecified():
3333

3434
def test_zero():
3535
assert curry(add_args, 0)() == 0
36+
37+
38+
def test_uncurry():
39+
assert uncurry(curry(add2))(1, 2) == 3
40+
41+
42+
def test_uncurry_incorrect():
43+
with pytest.raises(Exception) as e:
44+
uncurry(curry(add2))(1, 2, 3)
45+
assert e == "incorrect amount of arguments provided"

0 commit comments

Comments
 (0)