This is definitely lensy. The monad is actually just a bit of a distraction because all you need is a functor:
changesecond (a, b) f = fmap (a,) (f b)
I'm pretty sure the _2
lens can be made to do your bidding with a basic lens thing like maybe over
but I'm not too familiar with the library yet.
Edit
No combinator is really needed. You can write
changesecond pair f = _2 f pair
You should be able to work this out from the general definition of the Lens
type.
Edit 2
This simple example demonstrates the main theme of Van Laarhoven lens construction:
- Extract the focus from the context.
- Apply the given function to produce a functorful of results.
- Use
fmap
to restore the contexts to the results.
Ed Kmett's lens
library elaborates on this theme in various ways. Sometimes it strengthens the functor constraint. Sometimes it generalizes the function to a profunctor. In the case of Equality
, it removes the functor constraint. It just turns out that the same basic type shape can express a lot of different ideas.