将一个集转换为Clojure中的集

时间:2014-09-08 22:37:01

标签: clojure

我想编写一个可以转换的函数:

 (#{"one" "two"})

#{"one" "two"}

我这样做是因为我有一个函数返回(#{"一个""两个"})作为for循环的结果。我想通过使用clojure.set / difference来获取结果集并将其与另一个进行比较。我不能,因为其中一个人有一套括号​​。

谢谢!

3 个答案:

答案 0 :(得分:1)

我假设:

  • 所需函数的输入是一个lazy seq(或其他一些序列),只包含一组
  • 函数的输出应该是set

已经存在一个能够满足您需求的功能:first。你可以阅读它here

答案 1 :(得分:0)

假设你的例子中你所拥有的是一套懒惰的集合,如:

(def data '(#{"one" "two"} #{"three"} #{"one"})); note the quote 

根据您的要求,您可能需要:

从所有集合中删除元素“one”:

   user> (map #(disj % "one") data)       
   (#{"two"} #{"three"} #{})

删除所有包含“one”一词的集:

   user> (remove #(% "one") data)
   (#{"three"})

将seq of sets转换为一组,并删除元素“one”

   user> (disj (set (apply concat data)) "one")
   #{"two" "three"}

从第一组seq中删除元素“one”

   user> (disj (first data) "one")
   #{"two"}
   user> (seq (disj (first data) "one")) ; this returns exactly what you ask for
   ("two")

答案 2 :(得分:0)

(clojure.set/difference (first '(#{"one" "two"})) #{"one"})

...生成#{"two"}

您可以将表达式拉出到函数中并应用它:

((fn [x y] (clojure.set/difference (first x)) y) '(#{"one" "two"}) #{"one"})