@@ -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
784783alterF :: (Functor f , Ord k )
785784 => (Maybe a -> f (Maybe a )) -> k -> Map k a -> f (Map k a )
0 commit comments