r/functionalprogramming • u/chandru89new • Oct 15 '25
FP Using WriterT / Writer to accumulate "effects" rather than just logging
in most examples i have seen of Writer monad, it's used when you want to return a computed value and a log of messages/string.
in a recent project, i wanted to make my game state updater a pure function (it was in
IO/Aff).i went about shopping for solutions and there were many: Free monads, effects library (polysemy, effects etc.), MTL-based.. and after some more digging in and looking for simpler solutions, i realized that I could use the writer monad.
Writer monad allows you to return a computed value (my updated game state) along with a list of some type 'a'. i just made that some type 'a' to be an ADT of "effects". because Writer allows me to "accumulate" the effects, i simply got my game state updater function to return a computed value and a list of effects.
a separate effects handler function took care of handling the effects. (discovered
tailRecMin this process).the main function, game's runtime, took care of running the Writer monad and then looping after effects were handled.
this allowed me to test my game state updater function so easily. (code in case you want to take a look)
credits: this is essentially the Elm Architecture except, expressing this in Haskell/Purescript seems much more cleaner, explicit and neat. i didnt realize it was mimicking TEA until i got to the point where i was handling the effects in the main function.
disclaimer: this is a TIL kind of a thing. YMMV and not advocating for this to be adopted on complex codebases... just that it fit neatly with my toy project requirement and i am looking forward to trying this out on some of my other sideprojects.
Duplicates
haskell • u/chandru89new • Oct 15 '25
hey folks, what are your thoughts on this? specifically, looking for learning gaps / pitfalls even as an implementation in a toy project.
purescript • u/chandru89new • Oct 15 '25