Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Last active January 4, 2016 12:59
Show Gist options
  • Select an option

  • Save danidiaz/8624735 to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/8624735 to your computer and use it in GitHub Desktop.
Experimenting with Control.Lens.At and "non"
ghci> at 1 ?~ "hello" $ M.empty
fromList [(1,"hello")]
ghci> at 1 .~ (Just "hello") $ M.empty
fromList [(1,"hello")]
ghci> at 1 .~ Nothing $ M.fromList [(1,"blah")]
fromList []
ghci> (%~) (at 2) (maybe (Just "b") (\x -> Just $ x ++ "bbb")) (M.fromList [(2,"a")])
fromList [(2,"abbb")]
ghci> Nothing ^. non 0
0
ghci> M.empty & at "hello" . non M.empty . at "world" .~ Just "!!!"
fromList [("hello",fromList [("world","!!!")])]
-- Beware, this in a bad use of "non"
ghci> M.fromList [("hello",0)] & at "hello" . non 0 %~ (const 0)
fromList []
-- Beware, this in a bad use of "non"
ghci> M.fromList [("hello",M.empty)] & at "hello" . non M.empty . at "world" .~ Just "!!!"
fromList [("hello",fromList [("world","!!!")])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment