Given a Writer
monad action, I want to modify it by mapping a function over the written data inside the monad action.
Something like:
retell :: (w -> w') -> Writer w a -> Writer w' a
Does such a function already exists in the libraries? If not, how can one be defined?
retell f = Writer . second f $ runWriter
There is also a mapWriter
function provided by the libraries. So you could do this:
retell = mapWriter . second
The second
function is in Control.Arrow
, but you can define a less general version of it yourself like this:
second f (a, b) = (a, f b)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With