From 45d1e4f4aa75c2e6df486e82d831903113122980 Mon Sep 17 00:00:00 2001 From: aranchelk Date: Sun, 8 Jun 2025 06:55:31 -0700 Subject: [PATCH 1/2] Improve documentation for Map functions with bounds params (#74) * Make it clear that the bounds are inclusive. Previously this was ambiguous in the prose and only indicated by the examples. * Generally tighten up the writing. --- src/Data/Map/Internal.purs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Data/Map/Internal.purs b/src/Data/Map/Internal.purs index 81cc398..39ae443 100644 --- a/src/Data/Map/Internal.purs +++ b/src/Data/Map/Internal.purs @@ -365,10 +365,9 @@ findMin = case _ of Leaf -> Just { key: k, value: v } _ -> findMin l --- | Fold over the entries of a given map where the key is between a lower and --- | an upper bound. Passing `Nothing` as either the lower or upper bound --- | argument means that the fold has no lower or upper bound, i.e. the fold --- | starts from (or ends with) the smallest (or largest) key in the map. +-- | Fold over the entries of a given map that have keys within a lower and +-- | upper bound (including keys equal to those bounds). `Nothing` is treated as +-- | unbounded. -- | -- | ```purescript -- | foldSubmap (Just 1) (Just 2) (\_ v -> [v]) @@ -420,10 +419,9 @@ foldSubmapBy appendFn memptyValue kmin kmax f = in go --- | Returns a new map containing all entries of the given map which lie --- | between a given lower and upper bound, treating `Nothing` as no bound i.e. --- | including the smallest (or largest) key in the map, no matter how small --- | (or large) it is. For example: +-- | Returns a new map containing all entries with keys that fall within a +-- | specified lower and upper bound (including keys equal to those bounds). +-- | `Nothing` is treated as unbounded. -- | -- | ```purescript -- | submap (Just 1) (Just 2) From 92a7c20fda232d0eb30d5ef0da87991fd27b0703 Mon Sep 17 00:00:00 2001 From: Thomas Cumming Date: Sat, 11 Apr 2026 23:11:57 +0100 Subject: [PATCH 2/2] Add set limit to range operation using submap (#75) --- CHANGELOG.md | 1 + src/Data/Set.purs | 6 ++++++ test/Test/Data/Set.purs | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 091f307..0adb6e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: +- Add `Data.Set.range` which works like `Data.Map.submap`. Bugfixes: diff --git a/src/Data/Set.purs b/src/Data/Set.purs index ef35ff6..7bd2755 100644 --- a/src/Data/Set.purs +++ b/src/Data/Set.purs @@ -18,6 +18,7 @@ module Data.Set , size , findMin , findMax + , range , union , unions , difference @@ -136,6 +137,11 @@ findMin (Set m) = Prelude.map _.key (M.findMin m) findMax :: forall a. Set a -> Maybe a findMax (Set m) = Prelude.map _.key (M.findMax m) +-- | Return the set limited to the lower and upper bounds (including keys equal +-- | to those bounds). +range :: forall a. Ord a => Maybe a -> Maybe a -> Set a -> Set a +range kmin kmax (Set m) = Set (M.submap kmin kmax m) + -- | Form the union of two sets -- | -- | Running time: `O(n + m)` diff --git a/test/Test/Data/Set.purs b/test/Test/Data/Set.purs index 0269206..2128b7a 100644 --- a/test/Test/Data/Set.purs +++ b/test/Test/Data/Set.purs @@ -37,3 +37,8 @@ setTests = do log "toggle - deletes item" assert $ S.toggle 1 (S.fromFoldable [1]) == S.empty + + log "range" + do let s1 = S.fromFoldable [1,2,3,4,5] + s2 = S.fromFoldable [2,3] + assert $ S.range (Just 2) (Just 3) s1 == s2