diff --git a/monads/__init__.py b/monads/__init__.py index 3c4c3d5..0b7e4a4 100644 --- a/monads/__init__.py +++ b/monads/__init__.py @@ -3,3 +3,4 @@ from .applicative import Applicative from .monad import Monad from .maybe import Maybe, Just, Nothing from .result import Result, Ok, Err +from .reader import Reader diff --git a/monads/reader.py b/monads/reader.py new file mode 100644 index 0000000..3c7f994 --- /dev/null +++ b/monads/reader.py @@ -0,0 +1,16 @@ +from __future__ import annotations +from typing import Any, Callable, Generic, TypeVar + +from .monad import Monad + +T = TypeVar("T") +S = TypeVar("S") +F = Callable[[T], S] + + +class Reader(Monad[T]): + def __init__(self, function: F) -> None: + self.function = function + + def map(self, function: F) -> Reader: + return Reader(lambda x: function(self.function(x)))