How can I typehint that something should have the same type as earlier?
def mymap(xs: [T], func: T -> V): -> [V]:
return [func(x) for x in xs]
How can I typehint that something should have the same type as earlier?
def mymap(xs: [T], func: T -> V): -> [V]:
return [func(x) for x in xs]
You can do this with TypeVar
from typing import Callable, List, TypeVar
T = TypeVar('T')
V = TypeVar('V')
def mymap(items: List[T], fn: Callable[[T], V]) -> List[V]:
return [fn(e) for e in items]