WIP: Reader

This commit is contained in:
Correl Roush 2018-10-12 17:02:56 -04:00
parent 63aec9aa75
commit ca7a2366b2
2 changed files with 17 additions and 0 deletions

View file

@ -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

16
monads/reader.py Normal file
View file

@ -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)))