Writer
funstruct.monad.writer
Writer monad — computations with accumulated output.
Built-in Writer types (each has its own Monoid):
ListWriter — output: list (combine = +, empty = [])
CListWriter — output: CList (combine = +, empty = Nil())
StrWriter — output: str (combine = +, empty = "")
IntWriter — output: int (combine = +, empty = 0)
Writer is unique among funstruct monads. Reader, State, Either, Option, and Result all have ONE type constructor with variants. Writer has MULTIPLE type constructors because each monoid creates a different type — ListWriter, StrWriter, IntWriter each need their own Monad instance.
Create custom Writers with Writer.for_monoid:
>>> from funstruct.monad.writer import Writer
>>> from funstruct.typeclasses import Monoid
>>> SetWriter = Writer.for_monoid(Monoid(typ=set, combine=lambda a, b: a | b, empty=set()))
Examples:
>>> from funstruct.monad.writer import ListWriter
>>> w = ListWriter(1, ["init"])
>>> w.map(lambda x: x + 10)
ListWriter(value=11, output=['init'])
>>> w.bind(lambda x: ListWriter(x + 1, ["inc"]))
ListWriter(value=2, output=['init', 'inc'])
>>> ListWriter.pure(99)
ListWriter(value=99, output=[])
Writer
Bases: DataType, Generic[_W, _A]
Writer: (A, W) with output combined via a class-level Monoid.
Source code in funstruct/monad/writer/__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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
do(gen_fn)
classmethod
Do-notation for Writer.
def pipeline(): ... x = yield ListWriter(1, ["init"]) ... y = yield ListWriter(x + 10, ["step"]) ... return x + y ListWriter.do(pipeline)() ListWriter(value=12, output=['init', 'step'])
Source code in funstruct/monad/writer/__init__.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
for_monoid(monoid, name=None)
classmethod
Create a Writer subclass for a specific Monoid.
Each returned class is its own type constructor with auto-registered Monad instance.
ListWriter = Writer.for_monoid(list_monoid)
CListWriter = Writer.for_monoid(clist_monoid)
Source code in funstruct/monad/writer/__init__.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |