Created
December 2, 2018 19:12
-
-
Save saulshanabrook/678ba212afab3a3553940c09475c5b3a to your computer and use it in GitHub Desktop.
Little example of how to have a typed ADT-ish compose in Python
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
import dataclasses | |
import typing | |
import typing_extensions | |
T = typing.TypeVar("T") | |
U = typing.TypeVar("U") | |
V = typing.TypeVar("V") | |
# Need this so that function attributes are not typed as methods | |
# https://github.com/python/mypy/issues/5485#issuecomment-441935633 | |
@dataclasses.dataclass | |
class Box(typing.Generic[T]): | |
inner: T | |
@property | |
def unboxed(self) -> T: | |
return self.inner | |
@dataclasses.dataclass | |
class Compose(typing.Generic[T, U, V]): | |
left: Box[typing.Callable[[U], V]] | |
right: Box[typing.Callable[[T], U]] | |
def __call__(self, arg: T) -> V: | |
return self.left.unboxed(self.right.unboxed(arg)) | |
def int_to_str(i: int) -> str: | |
return str(i) | |
def str_to_int(s: str) -> int: | |
return int(s) | |
identity = Compose(Box(str_to_int), Box(int_to_str)) | |
print(identity) | |
h: int = identity(123) | |
print(h) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment