Skip to content

Commit

Permalink
Fix discrepancy between mergeWithKey impl and docs
Browse files Browse the repository at this point in the history
The given `only` functions should only be called on nonempty trees.
Fixed for both Map and IntMap.
  • Loading branch information
meooow25 committed Aug 13, 2024
1 parent 96a8dec commit 98c03c9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions containers/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
`Data.IntSet.splitMember` are now strict in the key. Previously, the key was
ignored for an empty map or set. (Soumik Sarkar)

### Bug fixes

* Fix an issue where `Data.Map.mergeWithKey`, `Data.Map.Strict.mergeWithKey`,
`Data.IntMap.mergeWithKey`, `Data.IntMap.Strict.mergeWithKey` would call
provided `only` functions with empty maps, contrary to documentation.
(Soumik Sarkar)

## Unreleased with `@since` annotation for 0.7.1:

### Additions
Expand Down
2 changes: 2 additions & 0 deletions containers/src/Data/IntMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,8 @@ mergeWithKey' bin' f g1 g2 = go
| otherwise = maybe_link k1 (g1 t1) k2 (g2 t2)
merge0 t1 _ Nil = g1 t1

go Nil Nil = Nil

go Nil t2 = g2 t2

maybe_link _ Nil _ t2 = t2
Expand Down
1 change: 1 addition & 0 deletions containers/src/Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,7 @@ mergeWithKey :: Ord k
-> Map k a -> Map k b -> Map k c
mergeWithKey f g1 g2 = go
where
go Tip Tip = Tip
go Tip t2 = g2 t2
go t1 Tip = g1 t1
go (Bin _ kx x l1 r1) t2 =
Expand Down
1 change: 1 addition & 0 deletions containers/src/Data/Map/Strict/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ mergeWithKey :: Ord k
-> Map k a -> Map k b -> Map k c
mergeWithKey f g1 g2 = go
where
go Tip Tip = Tip
go Tip t2 = g2 t2
go t1 Tip = g1 t1
go (Bin _ kx x l1 r1) t2 =
Expand Down

0 comments on commit 98c03c9

Please sign in to comment.