1.3.0 permalink Arrow_down_16x16
  • (set coll)
Returns a set of the distinct elements of coll.

2 Examples top

  • ;; returns distinct elements
    user=> (set '(1 1 2 3 2 4 5 5))
    #{1 2 3 4 5}
    
    ;; returns distinct elements (different nomenclature)
    user=> (set [1 1 2 3 2 4 5 5])
    #{1 2 3 4 5}
    
    user=> (set [1 2 3 4 5])  
    #{1 2 3 4 5}
    
    user=> (set "abcd")
    #{\a \b \c \d}
    
    user=> (set '("a" "b" "c" "d"))
    #{"a" "b" "c" "d"}
    
    user=> (set {:one 1 :two 2 :three 3})
    #{[:two 2] [:three 3] [:one 1]}
    
  • (set [1 2 3 2 1 2 3])
    -> #{1 2 3}
    
    #{:a :b :c :d}
    -> #{:d :a :b :c}
    
    (hash-set :a :b :c :d)
    -> #{:d :a :b :c}
     
    (sorted-set :a :b :c :d)
    -> #{:a :b :c :d}
    
    ;------------------------------------------------
    
    (def s #{:a :b :c :d})
    (conj s :e)
    -> #{:d :a :b :e :c}
     
    (count s)
    -> 4
     
    (seq s)
    -> (:d :a :b :c)
     
    (= (conj s :e) #{:a :b :c :d :e})
    -> true
    
    (s :b)
    -> :b
     
    (s :k)
    -> nil
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:3637 top

(defn set
  "Returns a set of the distinct elements of coll."
  {:added "1.0"
   :static true}
  [coll] (clojure.lang.PersistentHashSet/create (seq coll)))
Vars in clojure.core/set:
Used in 0 other vars

Comments top

No comments for set. Log in to add a comment.