git.iko.soy/monoidal-containers
on master help sign in
Merge pull request #7 from qrilka/map-filterwithkey

Add map, filterWithKey

commitb85df0942d47debdd1a09582f652f8148d26acbd authorBen Gamari <ben@smart-cactus.org> dateMon 4 Jul 2016, 12:40 (10 years ago) parent8aa560e9685a2a
1 file changed+13
src/Data/HashMap/Monoidal.hs+13
src/Data/HashMap/Monoidal.hs+13
@@ -30,6 +30,8 @@ module Data.HashMap.Monoidal
3030 , mapKeys
3131 , modify
3232 , modifyDef
33+ , map
34+ , filterWithKey
3335 ) where
3436
3537import Prelude hiding (lookup)
@@ -189,3 +191,14 @@ mapKeys :: (Monoid a, Hashable k, Eq k, Hashable k', Eq k')
189191mapKeys f = fromList
190192 . map (\(k, v) -> (f k, v))
191193 . toList
194+
195+-- | /O(n)/ Filter this map by retaining only elements satisfying a
196+-- predicate.
197+filterWithKey :: (k -> v -> Bool) -> MonoidalHashMap k v -> MonoidalHashMap k v
198+filterWithKey pred = pack . HM.filterWithKey pred . unpack
199+{-# INLINE filterWithKey #-}
200+
201+-- | /O(n)/ Transform this map by applying a function to every value.
202+map :: (v1 -> v2) -> MonoidalHashMap k v1 -> MonoidalHashMap k v2
203+map f = pack . HM.map f . unpack
204+{-# INLINE map #-}