如何获取clojure中两个整数之间的排序集中的数字序列?

Gre*_*ers 6 clojure

假设我有一个整数排序集xs,我想检索xs中所有[x,y)的整数,即.在x和y之间.

我可以:

(select #(and (>= % x) (< % y)) xs)
Run Code Online (Sandbox Code Playgroud)

但这是低效的 - O(n)当它可能是O(log n)时,我期望返回的元素数量很少.使用take-while和drop-while会让我在到达y后退出,但我仍然无法有效地跳转到x.

我只是在学习clojure所以这里是我将如何在C++中做到这一点:

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

我可以在clojure中这样做吗?

Gre*_*ers 8

哎呀!我错过了subseq函数,没有从文档的数据结构页面链接到它.

(subseq xs >= x < y)
Run Code Online (Sandbox Code Playgroud)