I'm reading this tutorial:
http://blog.jakubarnold.cz/2014/08/06/lens-tutorial-stab-traversal-part-2.html
and my code looks like this:
import Control.Applicative
import Data.Traversable
import Control.Lens
data User = User String [Post] deriving Show
data Post = Post String deriving Show
posts :: Lens' User [Post]
posts f (User n p) = fmap (User n) (f p)
users :: [User]
users = [User "john" [Post "hello", Post "world"], User "bob" [Post "foobar"]]
tp :: (Traversable t, Applicative f) => ([Post] -> f [Post]) -> t User -> f (t User)
tp = traverse . posts
Now following the blog post, here are some common lens computations:
*Main> view tp users
[Post "hello",Post "world",Post "foobar"]
*Main> set tp [Post "x",Post "y"] users
[User "john" [Post "x",Post "y"],User "bob" [Post "x",Post "y"]]
*Main> view tp (set tp [Post "x",Post "y"] users)
[Post "x",Post "y",Post "x",Post "y"]
The last evaluation has left me confused. Isn't the following lens law supposed to hold?
view l (set l v s) = v
(The law is taken from http://artyom.me/lens-over-tea-2.)
This is a lens law, and tp is a traversal, so it doesn't have to abide by this law.
Strictly speaking, view shouldn't work with traversals at all (it works anyway, but it does so by combining the gathered results monoidally).
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