Reader
funstruct.monad.reader
Reader monad — computations that read from a shared environment.
Examples:
>>> from funstruct.monad.reader import Reader
>>> get_host = Reader(lambda cfg: cfg["host"])
>>> get_port = Reader(lambda cfg: cfg["port"])
>>> get_path = Reader(lambda cfg: cfg.get("path", "/"))
>>> build_url = (
... get_host
... .bind(lambda h: get_port
... .bind(lambda p: get_path
... .map(lambda path: f"http://{h}:{p}{path}")))
... )
>>> build_url.run({"host": "localhost", "port": 8080, "path": "/api"})
'http://localhost:8080/api'
>>> build_url.run({"host": "prod.co", "port": 443})
'http://prod.co:443/'
Reader
Bases: DataType, Generic[_Ctx, _A]
Reader: Ctx -> A.
Source code in funstruct/monad/reader/__init__.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
do(gen_fn)
classmethod
Do-notation via generators. Returns a callable.
def pipeline(): ... x = yield Reader(lambda ctx: ctx["x"]) ... y = yield Reader(lambda ctx: ctx["y"]) ... return x + y Reader.do(pipeline)().run({"x": 1, "y": 10}) 11
Source code in funstruct/monad/reader/__init__.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |