Using 'lens' for "over"-like computation in Monad

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using 'lens' for "over"-like computation in Monad

Nikolay Amiantov
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.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Using 'lens' for "over"-like computation in Monad

Nikolay Amiantov

Thank you! I'm still lost in the forest of all lens functions and generality, and haven't expected this signature. Can someone recommend good reading on all the lens-related types and classes,  what they represent and how can they be used? (interested not only in "how", but also "why")

30 авг. 2014 г. 11:20 пользователь "Benno Fünfstück" <[hidden email]> написал:
The lens operator that does this is %%~, also called `traverseOf`:

λ: ("Hello", "World") & _1 %%~ (\a -> putStrLn a >> return a)
Hello
("Hello","World")


--
Benno


2014-08-30 9:44 GMT+02:00 Nikolay Amiantov <[hidden email]>:
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.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe