Skip to content

Instantly share code, notes, and snippets.

@saulshanabrook
Created December 2, 2018 19:12
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 saulshanabrook/678ba212afab3a3553940c09475c5b3a to your computer and use it in GitHub Desktop.
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
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