Skip to content

Commit 912e594

Browse files
Feat: Add new advanced variadic generics challenge, inspired by scala's 'tupled' function
1 parent dafb4d3 commit 912e594

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check out [TypeVarTuple](https://docs.python.org/3/library/typing.html#typing.TypeVarTuple).
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
TODO:
3+
4+
Define an "tupled" function that accepts a function and returns a function with some arguments in the form of a tuple.
5+
The return type should remain unchanged.
6+
(if you're familiar with scala, this is the "tupled" function)
7+
"""
8+
9+
10+
def tupled(func):
11+
def impl(args):
12+
return func(*args)
13+
14+
return impl
15+
16+
17+
## End of your code ##
18+
from typing import Any
19+
20+
21+
def func0() -> Any:
22+
...
23+
24+
25+
def func1(s: str) -> Any:
26+
...
27+
28+
29+
def func2(s: str, i: int) -> Any:
30+
...
31+
32+
33+
func0_tupled = tupled(func0)
34+
func0_tupled(())
35+
func0_tupled() # expect-type-error
36+
37+
func1_tupled = tupled(func1)
38+
func1_tupled(("a",))
39+
func1_tupled("a") # expect-type-error
40+
func1_tupled(1) # expect-type-error
41+
42+
func2_tupled = tupled(func2)
43+
func2_tupled(("a", 1))
44+
func2_tupled(("a", "b")) # expect-type-error
45+
func2_tupled("a", 1) # expect-type-error
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
TODO:
3+
4+
Define an "tupled" function that accepts a function and returns a function with some arguments in the form of a tuple.
5+
The return type should remain unchanged.
6+
(if you're familiar with scala, this is the "tupled" function)
7+
"""
8+
from typing import Callable
9+
10+
11+
def tupled[*Ts, R](func: Callable[[*Ts], R]) -> Callable[[tuple[*Ts]], R]:
12+
def impl(args: tuple[*Ts]) -> R:
13+
return func(*args)
14+
15+
return impl
16+
17+
18+
## End of your code ##
19+
from typing import Any
20+
21+
22+
def func0() -> Any:
23+
...
24+
25+
26+
def func1(s: str) -> Any:
27+
...
28+
29+
30+
def func2(s: str, i: int) -> Any:
31+
...
32+
33+
34+
func0_tupled = tupled(func0)
35+
func0_tupled(())
36+
func0_tupled() # expect-type-error
37+
38+
func1_tupled = tupled(func1)
39+
func1_tupled(("a",))
40+
func1_tupled("a") # expect-type-error
41+
func1_tupled(1) # expect-type-error
42+
43+
func2_tupled = tupled(func2)
44+
func2_tupled(("a", 1))
45+
func2_tupled(("a", "b")) # expect-type-error
46+
func2_tupled("a", 1) # expect-type-error

0 commit comments

Comments
 (0)