Skip to content

Commit c3ee673

Browse files
committed
Make lazy map lazy in the structure
Previously, the lazy version of `mapWithKey` forced the whole structure (except the leaves). For a large `HashMap`, it's usually much better to be incremental. This also makes `mapWithKey` consistent with `traverseWithKey`. Fixes #195
1 parent b5f24e7 commit c3ee673

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Data/HashMap/Base.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,8 +1418,10 @@ mapWithKey f = go
14181418
where
14191419
go Empty = Empty
14201420
go (Leaf h (L k v)) = Leaf h $ L k (f k v)
1421-
go (BitmapIndexed b ary) = BitmapIndexed b $ A.map' go ary
1422-
go (Full ary) = Full $ A.map' go ary
1421+
go (BitmapIndexed b ary) = BitmapIndexed b $ A.map go ary
1422+
go (Full ary) = Full $ A.map go ary
1423+
-- Why map strictly over collision arrays? Because there's no
1424+
-- point suspending the O(1) work this does for each leaf.
14231425
go (Collision h ary) = Collision h $
14241426
A.map' (\ (L k v) -> L k (f k v)) ary
14251427
{-# INLINE mapWithKey #-}

0 commit comments

Comments
 (0)