@@ -18,6 +18,8 @@
1818module Data.HashMap.Monoidal
1919 ( MonoidalHashMap
2020 -- * Often-needed functions
21+ , toList
22+ , fromList
2123 , singleton
2224 , size
2325 , member
@@ -34,7 +36,7 @@ module Data.HashMap.Monoidal
3436 , filterWithKey
3537 ) where
3638
37−import Prelude hiding (lookup)
39+import Prelude hiding (lookup, map)
3840import Data.Maybe (fromMaybe)
3941import Data.Monoid
4042import Data.Foldable (Foldable)
@@ -43,7 +45,7 @@ import Data.Data (Data)
4345import Data.Typeable (Typeable)
4446
4547#if MIN_VERSION_base(4,7,0)
46−import GHC.Exts (IsList(..))
48+import qualified GHC.Exts as Exts
4749#endif
4850
4951import Control.DeepSeq
@@ -103,7 +105,7 @@ instance Newtype (MonoidalHashMap k a) (M.HashMap k a) where
103105 {-# INLINE unpack #-}
104106
105107#if MIN_VERSION_base(4,7,0)
106−instance (Eq k, Hashable k, Monoid a) => IsList (MonoidalHashMap k a) where
108+instance (Eq k, Hashable k, Monoid a) => Exts.IsList (MonoidalHashMap k a) where
107109 type Item (MonoidalHashMap k a) = (k, a)
108110 fromList = MM . M.fromListWith mappend
109111 {-# INLINE fromList #-}
@@ -162,6 +164,18 @@ keys :: MonoidalHashMap k a -> [k]
162164keys = M.keys . unpack
163165{-# INLINE keys #-}
164166
167+-- | /O(n*log n)/. Construct a map with the supplied mappings. If the list
168+-- contains duplicate mappings, the later mappings take precedence.
169+fromList :: (Eq k, Hashable k) => [(k,a)] -> MonoidalHashMap k a
170+fromList = pack . M.fromList
171+{-# INLINE fromList #-}
172+
173+-- | /O(n*log n)/. Construct a map with the supplied mappings. If the list
174+-- contains duplicate mappings, the later mappings take precedence.
175+toList :: MonoidalHashMap k a -> [(k,a)]
176+toList = M.toList . unpack
177+{-# INLINE toList #-}
178+
165179-- | /O(log n)/. Modify a value on some key with a function, if value
166180-- under key doesn't exist -- use mempty.
167181modify :: (Monoid a, Hashable k, Eq k)
@@ -186,19 +200,19 @@ modifyDef d f k = pack
186200
187201-- | /O(n)/. Map a function to each key of a map
188202mapKeys :: (Monoid a, Hashable k, Eq k, Hashable k', Eq k')
189− => (k -> k') -> MonoidalHashMap k a
190− -> MonoidalHashMap k' a
203+ => (k -> k') -> MonoidalHashMap k a -> MonoidalHashMap k' a
191204mapKeys f = fromList
192− . map (\(k, v) -> (f k, v))
205+ . fmap (\(k, v) -> (f k, v))
193206 . toList
207+{-# INLINE mapKeys #-}
194208
195209-- | /O(n)/ Filter this map by retaining only elements satisfying a
196210-- predicate.
197211filterWithKey :: (k -> v -> Bool) -> MonoidalHashMap k v -> MonoidalHashMap k v
198−filterWithKey pred = pack . HM.filterWithKey pred . unpack
212+filterWithKey pred = pack . M.filterWithKey pred . unpack
199213{-# INLINE filterWithKey #-}
200214
201215-- | /O(n)/ Transform this map by applying a function to every value.
202216map :: (v1 -> v2) -> MonoidalHashMap k v1 -> MonoidalHashMap k v2
203−map f = pack . HM.map f . unpack
217+map f = pack . M.map f . unpack
204218{-# INLINE map #-}