Skip to content

Commit

Permalink
Extend Foldable (foldl', null, length) (#223)
Browse files Browse the repository at this point in the history
* extend Foldable (foldl', null, length)

* ghc 7.8.4 Foldable compat
  • Loading branch information
shekeru authored and treeowl committed Feb 12, 2019
1 parent a3373e9 commit 53eb7eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Data/HashMap/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,16 @@ instance Functor (HashMap k) where
fmap = map

instance Foldable.Foldable (HashMap k) where
foldr f = foldrWithKey (const f)
foldr = Data.HashMap.Base.foldr
{-# INLINE foldr #-}
foldl' = Data.HashMap.Base.foldl'
{-# INLINE foldl' #-}
#if MIN_VERSION_base(4,8,0)
null = Data.HashMap.Base.null
{-# INLINE null #-}
length = Data.HashMap.Base.size
{-# INLINE length #-}
#endif

#if __GLASGOW_HASKELL__ >= 711
instance (Eq k, Hashable k) => Semigroup (HashMap k v) where
Expand Down Expand Up @@ -1530,7 +1539,7 @@ intersectionWithKey f a b = foldlWithKey' go empty a
-- | /O(n)/ Reduce this map by applying a binary operator to all
-- elements, using the given starting value (typically the
-- left-identity of the operator). Each application of the operator
-- is evaluated before using the result in the next application.
-- is evaluated before using the result in the next application.
-- This function is strict in the starting value.
foldl' :: (a -> v -> a) -> a -> HashMap k v -> a
foldl' f = foldlWithKey' (\ z _ v -> f z v)
Expand All @@ -1539,7 +1548,7 @@ foldl' f = foldlWithKey' (\ z _ v -> f z v)
-- | /O(n)/ Reduce this map by applying a binary operator to all
-- elements, using the given starting value (typically the
-- left-identity of the operator). Each application of the operator
-- is evaluated before using the result in the next application.
-- is evaluated before using the result in the next application.
-- This function is strict in the starting value.
foldlWithKey' :: (a -> k -> v -> a) -> a -> HashMap k v -> a
foldlWithKey' f = go
Expand Down
10 changes: 10 additions & 0 deletions Data/HashSet/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ instance Ord1 HashSet where
instance Foldable.Foldable HashSet where
foldr = Data.HashSet.Base.foldr
{-# INLINE foldr #-}
foldl' = Data.HashSet.Base.foldl'
{-# INLINE foldl' #-}
#if MIN_VERSION_base(4,8,0)
toList = Data.HashSet.Base.toList
{-# INLINE toList #-}
null = Data.HashSet.Base.null
{-# INLINE null #-}
length = Data.HashSet.Base.size
{-# INLINE length #-}
#endif

#if __GLASGOW_HASKELL__ >= 711
instance (Hashable a, Eq a) => Semigroup (HashSet a) where
Expand Down

0 comments on commit 53eb7eb

Please sign in to comment.