![](/img/trans.png)
[英]Two java.util.Iterators to the same collection: do they have to return elements in the same order?
[英]Return same elements of two sets in Clojure
我有两套:
#{1 2 3}
和#{7 8 3}
我想创建一个仅返回每个集合3的共享值的函数。
我不能使用十字路口; 它不适用于当前版本的clojure。
谢谢!
据我clojure.set/intersection
函数已经存在很久了,您确定使用正确吗? 这绝对应该工作:
(require '[clojure.set])
(clojure.set/intersection #{1 2 3} #{7 8 3})
=> #{3}
或者也许是时候更新到Clojure的更新版本了...
我不明白为什么无法使用clojure.set/intersection
,但是您可以定义自己的。
一个简单的实现是
(defn intersection [x y]
(into #{} (filter (partial contains? x) y)))
给予
(intersection #{1 2 3} #{7 8 3})
;#{3}
您可以在此处找到更完整,更快的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.