Skip to content

Consider adding a range function for Sets and Maps #167

Closed
@w9

Description

@w9

Maybe it's my ignorance, but currently I don't know if there's an succinct way to extract a range of elements from a set (in O(Log n) time). I know I can use split to achieve the effect:

import qualified Data.Set as S

-- The lower bound is inclusive and the upper bound is exclusive.
range' :: Ord a => (a, a) -> S.Set a -> S.Set a
range' (lower, upper) s = 
    let (_, e, right) = S.splitMember lower s
        (left, _)     = S.split upper right
    in  if e
          then S.union (S.singleton lower) left
          else left

main :: IO ()
main = do
    print $ range' (0, 3) (S.fromList [-1,2,0,1,5,6,3,7])
    return ()

-- output:
--   fromList [0,1,2]

But since this is a very common operation, can you add this function to the library? (For both Set and Map)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions