Skip to content

Add new type challenge based on scala's tupled function #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions challenges/advanced-variadic-generics2/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check out [TypeVarTuple](https://docs.python.org/3/library/typing.html#typing.TypeVarTuple).
45 changes: 45 additions & 0 deletions challenges/advanced-variadic-generics2/question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
TODO:

Define an "tupled" function that accepts a function and returns a function with some arguments in the form of a tuple.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description is not so clear on what it expects user to do, and why
I'd suggest give it an example or put it in a more practical scene

The return type should remain unchanged.
(if you're familiar with scala, this is effectively the "tupled" function)
"""


def tupled(func):
def impl(args):
return func(*args)

return impl


## End of your code ##
from typing import Any


def func0() -> Any:
...


def func1(s: str) -> Any:
...


def func2(s: str, i: int) -> Any:
...


func0_tupled = tupled(func0)
func0_tupled(())
func0_tupled() # expect-type-error

func1_tupled = tupled(func1)
func1_tupled(("a",))
func1_tupled("a") # expect-type-error
func1_tupled(1) # expect-type-error

func2_tupled = tupled(func2)
func2_tupled(("a", 1))
func2_tupled(("a", "b")) # expect-type-error
func2_tupled("a", 1) # expect-type-error
46 changes: 46 additions & 0 deletions challenges/advanced-variadic-generics2/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
TODO:

Define an "tupled" function that accepts a function and returns a function with some arguments in the form of a tuple.
The return type should remain unchanged.
(if you're familiar with scala, this is effectively the "tupled" function)
"""
from typing import Callable


def tupled[*Ts, R](func: Callable[[*Ts], R]) -> Callable[[tuple[*Ts]], R]:
def impl(args: tuple[*Ts]) -> R:
return func(*args)

return impl


## End of your code ##
from typing import Any


def func0() -> Any:
...


def func1(s: str) -> Any:
...


def func2(s: str, i: int) -> Any:
...


func0_tupled = tupled(func0)
func0_tupled(())
func0_tupled() # expect-type-error

func1_tupled = tupled(func1)
func1_tupled(("a",))
func1_tupled("a") # expect-type-error
func1_tupled(1) # expect-type-error

func2_tupled = tupled(func2)
func2_tupled(("a", 1))
func2_tupled(("a", "b")) # expect-type-error
func2_tupled("a", 1) # expect-type-error
Loading