Skip to content

Instantly share code, notes, and snippets.

@ltbringer
Created October 22, 2022 13:58
Show Gist options
  • Save ltbringer/86849936612972c84fc236d84263a5c8 to your computer and use it in GitHub Desktop.
Save ltbringer/86849936612972c84fc236d84263a5c8 to your computer and use it in GitHub Desktop.
Function composition with reverse 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 __rmul__(self, g: a1) -> a1:
return Compose(lambda x: g(self.f(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