Skip to content

Commit 5b0f77c

Browse files
committed
Add strict foldlWithKeyM
1 parent 0b41e11 commit 5b0f77c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

containers/src/Data/Map/Internal.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ module Data.Map.Internal (
262262
, foldrWithKey'
263263
, foldlWithKey'
264264

265+
-- ** Strict monadic folds
266+
, foldlWithKeyM'
267+
265268
-- * Conversion
266269
, elems
267270
, keys
@@ -3362,6 +3365,17 @@ foldlWithKey' f z = go z
33623365
in go (f z'' kx x) r
33633366
{-# INLINE foldlWithKey' #-}
33643367

3368+
-- | /O(n)/. Monadic variant of 'foldlWithKey\''.
3369+
foldlWithKeyM' :: Monad m => (a -> k -> b -> m a) -> a -> Map k b -> m a
3370+
foldlWithKeyM' f z = go z
3371+
where
3372+
go !z' Tip = return z'
3373+
go z' (Bin _ kx x l r) = do
3374+
!z'' <- go z' l
3375+
z''' <- f z'' kx x
3376+
go z''' r
3377+
{-# INLINE foldlWithKeyM' #-}
3378+
33653379
-- | /O(n)/. Fold the keys and values in the map using the given monoid, such that
33663380
--
33673381
-- @'foldMapWithKey' f = 'Prelude.fold' . 'mapWithKey' f@

0 commit comments

Comments
 (0)