c++ - 相当于 clojure 中 C++ 的 stable_partition?

标签 c++ clojure

在 C++ 中,标准库中有一个名为 stable_partition 的函数,它接受一个集合和一个谓词。它将集合进行划分,并将谓词返回 true 的元素放置在一个位置,将谓词返回 false 的元素放置在另一位置,同时保留元素的相对顺序。

我只是想知道标准 clojure 库中是否有类似的东西。尽管我进行了搜索,但我找不到这样的功能。它可能返回两个较小集合的惰性序列,其中一个集合包含谓词返回 true 的元素,另一个集合包含谓词返回 false 的元素。

它可能看起来像这样:

(稳定分区偶数? [1 2 3 4 5]) -> ([1 3 5] [2 4])

最佳答案

我认为最简单的版本是:

(defn stable-partition [p? coll]
  (map (group-by p? coll) [false true]))

既然你这么说了

"It divides the collection and puts those elements for which the predicate returns true in one place and those elements for which the predicate returns false in another place"

也许group-by本身就是答案(因为 HashMap 中键下的 vector 毕竟是位置)。

关于c++ - 相当于 clojure 中 C++ 的 stable_partition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244158/

相关文章:

c++ - 将类型转换应用于列表 : is "const" special?

clojure - clojure 中的 clj-webdriver : where to find taxi. clj?

clojure - 如何在Clojure中找到此向量的最小成员的索引?

memory-leaks - 如何在 Clojure 中泄漏内存?

c++ - 使函数异常安全

c++ - 如何创建可以采用函数指针和 lambda 的模板函数

c++ - 错误 : expected constructor, 析构函数,或 ';' token 之前的类型转换?

c++ - 这个失败的测试是否将零添加到空指针未定义的行为、编译器错误或其他什么?

clojure - 为什么 leiningen 下载 1GB(几乎)的 jar 索引以及如何阻止它?

macros - 在 clojure 中恢复到宏中的全局范围?