Editing Written Data in Monad Writer

Given the action of monad Writer , I want to change it by mapping a function over the recorded data inside the monad action.

Sort of:

 retell :: (w -> w') -> Writer wa -> Writer w' a 

Does such a function exist in libraries? If not, how can you determine?

+6
source share
1 answer
 retell f = Writer . second f $ runWriter 

There is also a mapWriter function provided by libraries. So you can do this:

 retell = mapWriter . second 

The second function is in Control.Arrow , but you can define its more general version as follows:

 second f (a, b) = (a, fb) 
+11
source

Source: https://habr.com/ru/post/916020/


All Articles