How do I get the sequence of numbers in a sorted-set that are between two integers in clojure?

Posted by Greg Rogers on Stack Overflow See other posts from Stack Overflow or by Greg Rogers
Published on 2010-06-03T04:42:44Z Indexed on 2010/06/03 4:44 UTC
Read the original article Hit count: 265

Filed under:

Say I have a sorted-set of integers, xs, and I want to retrieve all the integers in xs that are [x, y), ie. between x and y.

I can do:

(select #(and (>= % x) (< % y)) xs)

But this is inefficient - O(n) when it could be O(k log n) where k is the number of integers returned.

I am just learning clojure so here is how I would do it in C++:

set<int>::iterator first = xs.lower_bound(x);
set<int>::iterator last = xs.upper_bound(y);
for (; first != last; ++first)
    // do something with *first

Can I do this in clojure?

© Stack Overflow or respective owner

Related posts about clojure