组装镜头时遇到麻烦

时间:2014-07-07 08:51:42

标签: haskell lens

首先,一段代码:

λ> let applicationState = ('a','b',(M.fromList $ zip [1..3] [11,22,33],M.fromList $ zip [4,5,6] [44,55,66],M.fromList $ zip [7,8,9] [S.fromList ["77","777","7777"],S.fromList ["88","888","8888"],S.fromList ["99","999","9999"]]))
λ> :t applicationState
applicationState
  :: (Char,
      Char,
      (M.Map Integer Integer,
       M.Map Integer Integer,
       M.Map Integer (S.Set [Char])))
λ> -- this doesn't work
λ> -- applicationState ^. _3 . _3 . at 9 . contains "9999"
λ> -- this does but only for getting, not for setting
λ> applicationState ^. _3 . _3 . at 9 . to (maybe False (S.member "9999"))
True

我被#haskell-lens频道的友好帮助指导使用最后一行,但我也希望能够设置。

任何帮助?

稍后编辑:这似乎有效:

λ> applicationState ^. _3 . _3 . at 9 . non S.empty . contains "99999"
False
λ> applicationState & _3 . _3 . at 9 . non S.empty . contains "99999" .~ True

1 个答案:

答案 0 :(得分:3)

to组合器仅创建Getters,因此您无法再设置。要进行设置,请使用_Just之类的棱镜代替to