Haskell语言学习笔记(65)Lens

Posted zwvista

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haskell语言学习笔记(65)Lens相关的知识,希望对你有一定的参考价值。

手动计算 set mapped 5 [1,2,3]

set l b = runIdentity . l (\_ -> Identity b)
mapped = sets fmap
sets f g = taintedDot (f (untaintedDot g))

instance Settable Identity where
  untainted = runIdentity
  untaintedDot = (runIdentity #.)
  taintedDot = (Identity #.)
set mapped 5 [1,2,3]
= runIdentity . (sets fmap) (\_ -> Identity 5) $ [1,2,3]
= runIdentity $ sets fmap (\_ -> Identity 5) $ [1,2,3]
= runIdentity $ taintedDot (fmap (untaintedDot (\_ -> Identity 5))) $ [1,2,3]
= runIdentity $ (Identity .) (fmap ((runIdentity .) (\_ -> Identity 5))) $ [1,2,3]
= runIdentity $ (Identity .) (fmap (fmap runIdentity (\_ -> Identity 5))) $ [1,2,3]
= runIdentity $ Identity . (fmap (\_ -> 5)) $ [1,2,3]
= runIdentity $ Identity $ fmap (\_ -> 5) [1,2,3]
= runIdentity $ Identity $ [5,5,5]
= [5,5,5]

以上是关于Haskell语言学习笔记(65)Lens的主要内容,如果未能解决你的问题,请参考以下文章

Haskell语言学习笔记(64)Lens

如何在 Haskell 中比较镜头

Control.Lens 是不是有性能开销?

Haskell 光学库——Optics

Haskell语言学习笔记(69)Yesod

非法镜头 Haskell