Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a set from a sequence in clojure

Tags:

clojure

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?

like image 315
zetafish Avatar asked Dec 07 '22 13:12

zetafish


1 Answers

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}
like image 110
Arthur Ulfeldt Avatar answered Dec 11 '22 09:12

Arthur Ulfeldt