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/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) 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