【问题标题】:Return same elements of two sets in Clojure在 Clojure 中返回两组相同的元素
【发布时间】:2014-08-25 21:37:24
【问题描述】:

我有两套:

#{1 2 3}#{7 8 3}

我想创建一个只返回每个集合的共享值 3 的函数。

我不能使用交集;它不适用于我当前版本的 clojure。

谢谢!

【问题讨论】:

  • 你的意思是 clojure.set/intersection 不适用于这个项目使用的 Clojure 版本吗?

标签: collections clojure set


【解决方案1】:

clojure.set/intersection 功能从我记事起就一直存在,您确定您使用正确吗?这绝对应该有效:

(require '[clojure.set])
(clojure.set/intersection #{1 2 3} #{7 8 3})
=> #{3}

或者也许是时候更新到更新版本的 Clojure...

【讨论】:

  • 我不需要 clojure.set。感谢您指出这一点!
【解决方案2】:

我不明白为什么您无法使用clojure.set/intersection,但您可以定义自己的。

一个简单的实现是

(defn intersection [x y]
  (into #{} (filter (partial contains? x) y)))

给予

(intersection #{1 2 3} #{7 8 3})
;#{3}

您可以找到更完整和更快的版本here

【讨论】:

    猜你喜欢
    • 2018-05-02
    • 2023-04-06
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多