Skip to content

Instantly share code, notes, and snippets.

@fabianvf
Created November 18, 2015 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabianvf/a671bc0a0d9ff14f5efb to your computer and use it in GitHub Desktop.
Save fabianvf/a671bc0a0d9ff14f5efb to your computer and use it in GitHub Desktop.
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