Skip to content

Commit 47c5c9d

Browse files
authored
Add practical examples in alterF Haddocks (#1216)
1 parent ffa9637 commit 47c5c9d

6 files changed

Lines changed: 85 additions & 61 deletions

File tree

containers/src/Data/IntMap/Internal.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,27 +1145,26 @@ alter f k Nil = case f Nothing of
11451145
-- or update a value in an 'IntMap'. In short : @'lookup' k \<$\> 'alterF' f k m = f
11461146
-- ('lookup' k m)@.
11471147
--
1148-
-- Example:
1149-
--
1150-
-- @
1151-
-- interactiveAlter :: Int -> IntMap String -> IO (IntMap String)
1152-
-- interactiveAlter k m = alterF f k m where
1153-
-- f Nothing = do
1154-
-- putStrLn $ show k ++
1155-
-- " was not found in the map. Would you like to add it?"
1156-
-- getUserResponse1 :: IO (Maybe String)
1157-
-- f (Just old) = do
1158-
-- putStrLn $ "The key is currently bound to " ++ show old ++
1159-
-- ". Would you like to change or delete it?"
1160-
-- getUserResponse2 :: IO (Maybe String)
1161-
-- @
1162-
--
11631148
-- 'alterF' is the most general operation for working with an individual
11641149
-- key that may or may not be in a given map.
11651150
--
11661151
-- Note: 'alterF' is a flipped version of the @at@ combinator from
11671152
-- @Control.Lens.At@.
11681153
--
1154+
-- === Examples
1155+
--
1156+
-- @
1157+
-- -- Lookup the value at the key, and also remove the existing value or set a new value.
1158+
-- lookupAndSet :: Key -> Maybe a -> IntMap a -> (Maybe a, IntMap a)
1159+
-- lookupAndSet k new = alterF (\\old -> (old, new)) k
1160+
-- @
1161+
--
1162+
-- @
1163+
-- -- Delete the value at the key. If it is absent the result is Nothing.
1164+
-- mustDelete :: Key -> IntMap a -> Maybe (IntMap a)
1165+
-- mustDelete = alterF (Nothing <$)
1166+
-- @
1167+
--
11691168
-- @since 0.5.8
11701169

11711170
alterF :: Functor f

containers/src/Data/IntMap/Strict/Internal.hs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -601,27 +601,26 @@ alter f !k t =
601601
-- or update a value in an 'IntMap'. In short : @'lookup' k \<$\> 'alterF' f k m = f
602602
-- ('lookup' k m)@.
603603
--
604-
-- Example:
605-
--
606-
-- @
607-
-- interactiveAlter :: Int -> IntMap String -> IO (IntMap String)
608-
-- interactiveAlter k m = alterF f k m where
609-
-- f Nothing = do
610-
-- putStrLn $ show k ++
611-
-- " was not found in the map. Would you like to add it?"
612-
-- getUserResponse1 :: IO (Maybe String)
613-
-- f (Just old) = do
614-
-- putStrLn $ "The key is currently bound to " ++ show old ++
615-
-- ". Would you like to change or delete it?"
616-
-- getUserResponse2 :: IO (Maybe String)
617-
-- @
618-
--
619604
-- 'alterF' is the most general operation for working with an individual
620605
-- key that may or may not be in a given map.
621-
606+
--
622607
-- Note: 'alterF' is a flipped version of the 'at' combinator from
623608
-- 'Control.Lens.At'.
624609
--
610+
-- === Examples
611+
--
612+
-- @
613+
-- -- Lookup the value at the key, and also remove the existing value or set a new value.
614+
-- lookupAndSet :: Key -> Maybe a -> IntMap a -> (Maybe a, IntMap a)
615+
-- lookupAndSet k new = alterF (\\old -> (old, new)) k
616+
-- @
617+
--
618+
-- @
619+
-- -- Delete the value at the key. If it is absent the result is Nothing.
620+
-- mustDelete :: Key -> IntMap a -> Maybe (IntMap a)
621+
-- mustDelete = alterF (Nothing <$)
622+
-- @
623+
--
625624
-- @since 0.5.8
626625

627626
alterF :: Functor f

containers/src/Data/IntSet/Internal.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,20 @@ pop x0 t0 = case go x0 t0 of
608608
--
609609
-- Note: 'alterF' is a variant of the @at@ combinator from "Control.Lens.At".
610610
--
611+
-- === Examples
612+
--
613+
-- @
614+
-- -- Get whether the element is a member, and also insert or remove it.
615+
-- getAndSet :: Key -> Bool -> IntSet -> (Bool, IntSet)
616+
-- getAndSet x new = alterF (\\old -> (old, new)) x
617+
-- @
618+
--
619+
-- @
620+
-- -- Delete the element. If it is absent the result is Nothing.
621+
-- mustDelete :: Key -> IntSet -> Maybe IntSet
622+
-- mustDelete = alterF (\\b -> if b then Just False else Nothing)
623+
-- @
624+
--
611625
-- @since 0.6.3.1
612626
alterF :: Functor f => (Bool -> f Bool) -> Key -> IntSet -> f IntSet
613627
alterF f k s = fmap choose (f member_)

containers/src/Data/Map/Internal.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,21 +1161,6 @@ data AreWeStrict = Strict | Lazy
11611161
-- or update a value in a 'Map'. In short: @'lookup' k \<$\> 'alterF' f k m = f
11621162
-- ('lookup' k m)@.
11631163
--
1164-
-- Example:
1165-
--
1166-
-- @
1167-
-- interactiveAlter :: Int -> Map Int String -> IO (Map Int String)
1168-
-- interactiveAlter k m = alterF f k m where
1169-
-- f Nothing = do
1170-
-- putStrLn $ show k ++
1171-
-- " was not found in the map. Would you like to add it?"
1172-
-- getUserResponse1 :: IO (Maybe String)
1173-
-- f (Just old) = do
1174-
-- putStrLn $ "The key is currently bound to " ++ show old ++
1175-
-- ". Would you like to change or delete it?"
1176-
-- getUserResponse2 :: IO (Maybe String)
1177-
-- @
1178-
--
11791164
-- 'alterF' is the most general operation for working with an individual
11801165
-- key that may or may not be in a given map. When used with trivial
11811166
-- functors like 'Identity' and 'Const', it is often slightly slower than
@@ -1196,6 +1181,20 @@ data AreWeStrict = Strict | Lazy
11961181
-- Note: 'alterF' is a flipped version of the @at@ combinator from
11971182
-- @Control.Lens.At@.
11981183
--
1184+
-- === Examples
1185+
--
1186+
-- @
1187+
-- -- Lookup the value at the key, and also remove the existing value or set a new value.
1188+
-- lookupAndSet :: Ord k => k -> Maybe a -> Map k a -> (Maybe a, Map k a)
1189+
-- lookupAndSet k new = alterF (\\old -> (old, new)) k
1190+
-- @
1191+
--
1192+
-- @
1193+
-- -- Delete the value at the key. If it is absent the result is Nothing.
1194+
-- mustDelete :: Ord k => k -> Map k a -> Maybe (Map k a)
1195+
-- mustDelete = alterF (Nothing <$)
1196+
-- @
1197+
--
11991198
-- @since 0.5.8
12001199
alterF :: (Functor f, Ord k)
12011200
=> (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)

containers/src/Data/Map/Strict/Internal.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -745,21 +745,6 @@ alter = go
745745
-- 'alterF' can be used to inspect, insert, delete, or update a value in a 'Map'.
746746
-- In short: @'lookup' k \<$\> 'alterF' f k m = f ('lookup' k m)@.
747747
--
748-
-- Example:
749-
--
750-
-- @
751-
-- interactiveAlter :: Int -> Map Int String -> IO (Map Int String)
752-
-- interactiveAlter k m = alterF f k m where
753-
-- f Nothing = do
754-
-- putStrLn $ show k ++
755-
-- " was not found in the map. Would you like to add it?"
756-
-- getUserResponse1 :: IO (Maybe String)
757-
-- f (Just old) = do
758-
-- putStrLn $ "The key is currently bound to " ++ show old ++
759-
-- ". Would you like to change or delete it?"
760-
-- getUserResponse2 :: IO (Maybe String)
761-
-- @
762-
--
763748
-- 'alterF' is the most general operation for working with an individual
764749
-- key that may or may not be in a given map. When used with trivial
765750
-- functors like 'Identity' and 'Const', it is often slightly slower than
@@ -780,6 +765,20 @@ alter = go
780765
-- Note: 'alterF' is a flipped version of the @at@ combinator from
781766
-- @Control.Lens.At@.
782767
--
768+
-- === Examples
769+
--
770+
-- @
771+
-- -- Lookup the value at the key, and also remove the existing value or set a new value.
772+
-- lookupAndSet :: Ord k => k -> Maybe a -> Map k a -> (Maybe a, Map k a)
773+
-- lookupAndSet k new = alterF (\\old -> (old, new)) k
774+
-- @
775+
--
776+
-- @
777+
-- -- Delete the value at the key. If it is absent the result is Nothing.
778+
-- mustDelete :: Ord k => k -> Map k a -> Maybe (Map k a)
779+
-- mustDelete = alterF (Nothing <$)
780+
-- @
781+
--
783782
-- @since 0.5.8
784783
alterF :: (Functor f, Ord k)
785784
=> (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)

containers/src/Data/Set/Internal.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,20 @@ pop x0 t0 = case go x0 t0 of
614614
--
615615
-- Note: 'alterF' is a variant of the @at@ combinator from "Control.Lens.At".
616616
--
617+
-- === Examples
618+
--
619+
-- @
620+
-- -- Get whether the element is a member, and also insert or remove it.
621+
-- getAndSet :: Ord a => a -> Bool -> Set a -> (Bool, Set a)
622+
-- getAndSet x new = alterF (\\old -> (old, new)) x
623+
-- @
624+
--
625+
-- @
626+
-- -- Delete the element. If it is absent the result is Nothing.
627+
-- mustDelete :: Ord a => a -> Set a -> Maybe (Set a)
628+
-- mustDelete = alterF (\\b -> if b then Just False else Nothing)
629+
-- @
630+
--
617631
-- @since 0.6.3.1
618632

619633
-- See Note [alterF implementation]

0 commit comments

Comments
 (0)