Skip to content

Instantly share code, notes, and snippets.

@ltbringer
Last active October 22, 2022 13:57
Show Gist options
  • Select an option

  • Save ltbringer/4282915b687deea8d7fad08b3d06c356 to your computer and use it in GitHub Desktop.

Select an option

Save ltbringer/4282915b687deea8d7fad08b3d06c356 to your computer and use it in GitHub Desktop.
Function composition with operator overloading
from typing import Any, Callable
a1 = Callable[[Any], Any]
class Compose:
def __init__(self, f: a1) -> None:
self.f = f
def __mul__(self, g: a1) -> a1:
return Compose(lambda x: self.f(g(x)))
def __call__(self, *args: Any, **kwds: Any) -> Any:
return self.f(*args, **kwds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment