![](/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.