Skip to content

add Array.foldlM' and foldlWithKeyM' #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Data/HashMap/Array.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module Data.HashMap.Array
-- * Folds
, foldl'
, foldr
, foldlM'

, thaw
, map
Expand Down Expand Up @@ -418,6 +419,18 @@ foldl' f = \ z0 ary0 -> go ary0 (length ary0) 0 z0
(# x #) -> go ary n (i+1) (f z x)
{-# INLINE foldl' #-}

foldlM' :: Monad m => (b -> a -> m b) -> b -> Array a -> m b
foldlM' f = \ z0 ary0 -> go ary0 (length ary0) 0 z0
where
go ary n i !z
| i >= n = return z
| otherwise
= case index# ary i of
(# x #) -> do
fzx <- f z x
go ary n (i+1) fzx
{-# INLINE foldlM' #-}

foldr :: (a -> b -> b) -> b -> Array a -> b
foldr f = \ z0 ary0 -> go ary0 (length ary0) 0 z0
where
Expand Down
12 changes: 12 additions & 0 deletions Data/HashMap/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module Data.HashMap.Base
-- * Folds
, foldl'
, foldlWithKey'
, foldlWithKeyM'
, foldr
, foldrWithKey

Expand Down Expand Up @@ -1551,6 +1552,17 @@ foldlWithKey' f = go
go z (Collision _ ary) = A.foldl' (\ z' (L k v) -> f z' k v) z ary
{-# INLINE foldlWithKey' #-}

-- | /O(n)/ Monadic version of 'foldlWithKey''.
foldlWithKeyM' :: Monad m => (a -> k -> v -> m a) -> a -> HashMap k v -> m a
foldlWithKeyM' f = go
where
go !z Empty = return z
go z (Leaf _ (L k v)) = f z k v
go z (BitmapIndexed _ ary) = A.foldlM' go z ary
go z (Full ary) = A.foldlM' go z ary
go z (Collision _ ary) = A.foldlM' (\ z' (L k v) -> f z' k v) z ary
{-# INLINE foldlWithKeyM' #-}

-- | /O(n)/ Reduce this map by applying a binary operator to all
-- elements, using the given starting value (typically the
-- right-identity of the operator).
Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module Data.HashMap.Lazy
-- * Folds
, foldl'
, foldlWithKey'
, foldlWithKeyM'
, foldr
, foldrWithKey

Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module Data.HashMap.Strict
-- * Folds
, foldl'
, foldlWithKey'
, foldlWithKeyM'
, foldr
, foldrWithKey

Expand Down
1 change: 1 addition & 0 deletions Data/HashMap/Strict/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module Data.HashMap.Strict.Base
-- * Folds
, foldl'
, foldlWithKey'
, foldlWithKeyM'
, HM.foldr
, foldrWithKey

Expand Down