gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:35:46 24 4
gpt4 key购买 nike

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

我可以:

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

但这效率低下 - 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

我可以在 clojure 中做到这一点吗?

最佳答案

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

(subseq xs >= x < y)

关于clojure - 如何获得 clojure 中两个整数之间的排序集中的数字序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2963358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com