我正在阅读本教程:
http://blog.jakubarnold.cz/2014/08/06/lens-tutorial-stab-traversal-part-2.html
我的代码如下:
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
现在关注博客文章,这里有一些常见的镜头计算:
*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"]
最后的评价让我感到困惑。以下镜头法是不是应该举行?
view l (set l v s) = v
答案 0 :(得分:4)
这是镜头法,tp
是遍历,所以它不必遵守这部法律。
严格来说,view
根本不应该使用遍历(无论如何都可行,但它通过单独合并收集的结果来实现)。