Skip to content

Commit fedcbdc

Browse files
authored
Add instance Bifoldable HashMap (#251)
1 parent 0d0a9d4 commit fedcbdc

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Data/HashMap/Base.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ import Control.Monad.ST (ST)
131131
import Data.Bits ((.&.), (.|.), complement, popCount)
132132
import Data.Data hiding (Typeable)
133133
import qualified Data.Foldable as Foldable
134+
#if MIN_VERSION_base(4,10,0)
135+
import Data.Bifoldable
136+
#endif
134137
import qualified Data.List as L
135138
import GHC.Exts ((==#), build, reallyUnsafePtrEquality#)
136139
import Prelude hiding (filter, foldl, foldr, lookup, map, null, pred)
@@ -222,6 +225,17 @@ instance Foldable.Foldable (HashMap k) where
222225
{-# INLINE length #-}
223226
#endif
224227

228+
#if MIN_VERSION_base(4,10,0)
229+
-- | @since UNRELEASED
230+
instance Bifoldable HashMap where
231+
bifoldMap f g = foldMapWithKey (\ k v -> f k `mappend` g v)
232+
{-# INLINE bifoldMap #-}
233+
bifoldr f g = foldrWithKey (\ k v acc -> k `f` (v `g` acc))
234+
{-# INLINE bifoldr #-}
235+
bifoldl f g = foldlWithKey (\ acc k v -> (acc `f` k) `g` v)
236+
{-# INLINE bifoldl #-}
237+
#endif
238+
225239
#if __GLASGOW_HASKELL__ >= 711
226240
-- | '<>' = 'union'
227241
--

tests/HashMapProperties.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module Main (main) where
77

88
import Control.Monad ( guard )
99
import qualified Data.Foldable as Foldable
10+
#if MIN_VERSION_base(4,10,0)
11+
import Data.Bifoldable
12+
#endif
1013
import Data.Function (on)
1114
import Data.Hashable (Hashable(hashWithSalt))
1215
import qualified Data.List as L
@@ -291,6 +294,23 @@ pFoldr = (L.sort . M.foldr (:) []) `eq` (L.sort . HM.foldr (:) [])
291294
pFoldl :: [(Int, Int)] -> Bool
292295
pFoldl = (L.sort . M.foldl (flip (:)) []) `eq` (L.sort . HM.foldl (flip (:)) [])
293296

297+
#if MIN_VERSION_base(4,10,0)
298+
pBifoldMap :: [(Int, Int)] -> Bool
299+
pBifoldMap xs = concatMap f (HM.toList m) == bifoldMap (:[]) (:[]) m
300+
where f (k, v) = [k, v]
301+
m = HM.fromList xs
302+
303+
pBifoldr :: [(Int, Int)] -> Bool
304+
pBifoldr xs = concatMap f (HM.toList m) == bifoldr (:) (:) [] m
305+
where f (k, v) = [k, v]
306+
m = HM.fromList xs
307+
308+
pBifoldl :: [(Int, Int)] -> Bool
309+
pBifoldl xs = reverse (concatMap f $ HM.toList m) == bifoldl (flip (:)) (flip (:)) [] m
310+
where f (k, v) = [k, v]
311+
m = HM.fromList xs
312+
#endif
313+
294314
pFoldrWithKey :: [(Int, Int)] -> Bool
295315
pFoldrWithKey = (sortByKey . M.foldrWithKey f []) `eq`
296316
(sortByKey . HM.foldrWithKey f [])
@@ -414,6 +434,11 @@ tests =
414434
, testGroup "folds"
415435
[ testProperty "foldr" pFoldr
416436
, testProperty "foldl" pFoldl
437+
#if MIN_VERSION_base(4,10,0)
438+
, testProperty "bifoldMap" pBifoldMap
439+
, testProperty "bifoldr" pBifoldr
440+
, testProperty "bifoldl" pBifoldl
441+
#endif
417442
, testProperty "foldrWithKey" pFoldrWithKey
418443
, testProperty "foldlWithKey" pFoldlWithKey
419444
, testProperty "foldrWithKey'" pFoldrWithKey'

0 commit comments

Comments
 (0)