[Haskell-cafe] Using 'lens' for "over"-like computation in Monad

Nikolay Amiantov nikoamia at gmail.com
Sat Aug 30 07:44:45 UTC 2014


Hello Cafe,

I'm trying to wrap my head around 'lens' library. My current exercise is to
modify something using Lens in monad. Say,

("Hello", "World") & _1 `myOp` (\a -> putStrLn a >> return a)

in IO, where myOp would be of type:

myOp :: Monad m => Lens s t a b -> (a -> m b) -> s -> m t

Of course I can write it myself, using combination of "view" and "set":

myOp lens f v = f (view lens v) >>= flip (set lens) v

(have not checked this, but something like that should do), but is there a
more elegant way?

Nikolay.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140830/b8c0d61b/attachment.html>


More information about the Haskell-Cafe mailing list