问题描述:

What is an idiomatic way to convert a sequence to a set in Clojure? E.g what do I fill in at the dots?

(let s [1 1 2 2 3 3]

...)

So that it produces:

#{1 2 3}

I come up with:

(let [s [1 1 2 2 3 3]]

(loop [r #{} s s]

(if (empty? s) r (recur (conj r (first s)) (rest s)))))

But that seems not the way to go? Is there a function already that does this?

网友答案:

most collections have a function that produces them form anything seqable:

(set [1 1 2 2 3 3])
#{1 2 3}

for more interesting cases the into function is good to know about:

(into #{1}  [2 2 3 3])
#{1 2 3}
网友答案:

From http://clojure.org/data_structures#Data Structures-Sets:

You can also get a set of the values in a collection using the set function:

(set [1 2 3 2 1 2 3])
#{1 2 3}
相关阅读:
Top