Skip to content

Instantly share code, notes, and snippets.

@YoEight
Created February 16, 2020 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoEight/52bf7648ed9088158584d599ee1aa59a to your computer and use it in GitHub Desktop.
Save YoEight/52bf7648ed9088158584d599ee1aa59a to your computer and use it in GitHub Desktop.
How delete for Map should be.
import Data.Map (Map)
import Data.Map as Map
--------------------------------------------------------------------------------
-- | I'm bad at naming thing however, we are going to use that datastructure
-- so we could lookup and delete in one single pass.
data Blob a b = Blob a b
--------------------------------------------------------------------------------
instance Functor (Blob a) where
fmap f (Blob a b) = Blob a (f b)
--------------------------------------------------------------------------------
deleteMap :: k -> Map k v -> (Maybe v, Map k v)
deleteMap key ms =
let Blob result newMs =
Map.alterF go key ms in
(result, newMs)
where
go Nothing = Blob Nothing Nothing
go (Just e) = Blob (Just e) Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment