Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Typed compose
from typing import Callable, TypeVar
T = TypeVar('T')
T2 = TypeVar('T2')
T3 = TypeVar('T3')
def compose(func1: Callable[[T], T2],
func2: Callable[[T3], T]) -> Callable[[T3], T2]:
def inner(arg: T3) -> T2:
return func1(func2(arg))
return inner
def plus2(x: int) -> int:
return x + 2
def minus2(x: int) -> float:
return float(x - 2)
add0_plus_first = compose(minus2, plus2)
add0_minus_first = compose(plus2, minus2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.