Created
January 9, 2019 10:10
-
-
Save antonagestam/3db02a747cf16e59a448bf852bc68d4b to your computer and use it in GitHub Desktop.
Typed functional composition
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 | |
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