packages feed

monoidal-containers 0.1.0.0 → 0.1.1.0

raw patch · 2 files changed

+46/−5 lines, 2 files

Files

monoidal-containers.cabal view
@@ -1,5 +1,5 @@ name:                monoidal-containers-version:             0.1.0.0+version:             0.1.1.0 synopsis:            Containers with monoidal accumulation description:         Containers with monoidal accumulation homepage:            http://github.com/bgamari/monoidal-containers
src/Data/HashMap/Monoidal.hs view
@@ -23,15 +23,20 @@     , member     , notMember     , lookup+    , lookupM     , elems     , keys+    , delete+    , mapKeys+    , modify+    , modifyDef     ) where  import Prelude hiding (lookup)+import Data.Maybe (fromMaybe) import Data.Monoid import Data.Foldable (Foldable)-import Data.Traversable (Traversable)-import Control.Applicative (Applicative, pure)+import Control.Applicative (pure) import Data.Data (Data) import Data.Typeable (Typeable) @@ -96,9 +101,9 @@     {-# INLINE unpack #-}  #if MIN_VERSION_base(4,7,0)-instance (Eq k, Hashable k) => IsList (MonoidalHashMap k a) where+instance (Eq k, Hashable k, Monoid a) => IsList (MonoidalHashMap k a) where     type Item (MonoidalHashMap k a) = (k, a)-    fromList = MM . M.fromList+    fromList = MM . M.fromListWith mappend     {-# INLINE fromList #-}     toList = M.toList . unpack     {-# INLINE toList #-}@@ -130,6 +135,12 @@ lookup k = M.lookup k . unpack {-# INLINE lookup #-} +-- | /O(log n)/ Return the value to which the specified key is mapped,+-- or mempty if this map contains no mapping for the key.+lookupM :: (Eq k, Hashable k, Monoid v) => k -> MonoidalHashMap k v -> v+lookupM k = fromMaybe mempty . M.lookup k . unpack+{-# INLINE lookupM #-}+ -- | /O(log n)/. Delete a key and its value from the map. When the key is not -- a member of the map, the original map is returned. delete :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> MonoidalHashMap k a@@ -148,3 +159,33 @@ keys :: MonoidalHashMap k a -> [k] keys = M.keys . unpack {-# INLINE keys #-}++-- | /O(log n)/. Modify a value on some key with a function, if value+-- under key doesn't exist -- use mempty.+modify :: (Monoid a, Hashable k, Eq k)+       => (a -> a)+       -> k -> MonoidalHashMap k a+       -> MonoidalHashMap k a+modify f k = pack+           . M.insertWith (\_ old -> f old) k (f mempty)+           . unpack+{-# INLINE modify #-}++-- | /O(log n)/. Modify a value on some key with a function, providing+-- a default value if that key doesn't exist.+modifyDef :: (Monoid a, Hashable k, Eq k)+          => a -> (a -> a)+          -> k -> MonoidalHashMap k a+          -> MonoidalHashMap k a+modifyDef d f k = pack+                . M.insertWith (\_ old -> f old) k d+                . unpack+{-# INLINE modifyDef #-}++-- | /O(n)/. Map a function to each key of a map+mapKeys :: (Monoid a, Hashable k, Eq k, Hashable k', Eq k')+        => (k -> k') -> MonoidalHashMap k a+        -> MonoidalHashMap k' a+mapKeys f = fromList+          . map (\(k, v) -> (f k, v))+          . toList