Created
October 22, 2022 13:58
-
-
Save ltbringer/86849936612972c84fc236d84263a5c8 to your computer and use it in GitHub Desktop.
Function composition with reverse operator overloading
This file contains hidden or 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 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