假设我有一个整数排序集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中这样做吗?
| 归档时间: |
|
| 查看次数: |
159 次 |
| 最近记录: |