Created
November 18, 2015 23:41
-
-
Save fabianvf/a671bc0a0d9ff14f5efb to your computer and use it in GitHub Desktop.
Typed compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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