Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More `non`-examples.

  • Loading branch information...
commit 0c727bbbcae4e191cf38ff50a1408e8d84e094f8 1 parent be87348
@ekmett authored
Showing with 14 additions and 0 deletions.
  1. +2 −0  src/Control/Lens/Internal.hs
  2. +12 −0 src/Control/Lens/Iso.hs
View
2  src/Control/Lens/Internal.hs
@@ -229,6 +229,8 @@ instance (a ~ b) => ComonadStore a (Context a b) where
seeks f (Context g a) = Context g (f a)
experiment f (Context g a) = g <$> f a
+
+
-- | This is used to characterize a 'Control.Lens.Traversal.Traversal'.
--
-- a.k.a. indexed Cartesian store comonad, indexed Kleene store comonad, or an indexed 'FunList'.
View
12 src/Control/Lens/Iso.hs
@@ -193,6 +193,18 @@ simple = isomorphic id id
--
-- >>> Map.fromList [] ^. at "hello" . non 0
-- 0
+--
+-- This combinator is particularly useful when working with nested maps, when you want to
+-- create the nested map when it is missing:
+--
+-- >>> Map.empty & at "hello" . non Map.empty . at "world" ?~ "!!!"
+-- fromList [("hello",fromList [("world","!!!")]]
+--
+-- and have deleting the last entry from the nested map
+-- to mean we should delete its entry from the surrounding one:
+--
+-- >>> fromList [("hello",fromList [("world","!!!")]] & at "hello" . non Map.empty . at "world" .~ Nothing
+-- fromList []
non :: Eq a => a -> Simple Iso (Maybe a) a
non a = iso (fromMaybe a) go where
Please sign in to comment.
Something went wrong with that request. Please try again.