Skip to content

Instantly share code, notes, and snippets.

@antonagestam
Created January 9, 2019 10:10
Show Gist options
  • Save antonagestam/3db02a747cf16e59a448bf852bc68d4b to your computer and use it in GitHub Desktop.
Save antonagestam/3db02a747cf16e59a448bf852bc68d4b to your computer and use it in GitHub Desktop.
Typed functional composition
from typing import Callable, TypeVar
import functools
A = TypeVar('A')
B = TypeVar('B')
C = TypeVar('C')
def combine(f: Callable[[B], C], g: Callable[[A], B]) -> Callable[[A], C]:
def h(x: A) -> C:
return f(g(x))
return h
def compose(*fns: Callable) -> Callable:
return functools.reduce(combine, fns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment