ClojureDocs

Nav

Namespaces

set

clojure.core

Available since 1.0 (source)
  • (set coll)
Returns a set of the distinct elements of coll.
2 Examples
;; 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]}

user=> (set nil)
#{}
(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
See Also

Returns a new hash set with supplied keys. Any equal keys are handled as if by repeated uses of c...

Added by boxie

Returns a new sorted set with supplied keys. Any equal keys are handled as if by repeated uses of...

Added by boxie

conj[oin]. Returns a new collection with the xs 'added'. (conj nil item) returns (item). (co...

Added by boxie

When passed 2 rels, returns the rel corresponding to the natural join. When passed an additional k...

Added by boxie

Returns a set of the elements for which pred is true

Added by boxie

Return a set that is the first set without elements of the remaining sets

Added by boxie

Return a set that is the intersection of the input sets

Added by boxie

Return a set that is the union of the input sets

Added by boxie

Returns a map of the distinct values of ks in the xrel mapped to a set of the maps in xrel with th...

Added by boxie

Returns a rel of the elements of xrel with only the keys in ks

Added by boxie

Returns a rel of the maps in xrel with the keys in kmap renamed to the vals in kmap

Added by boxie

Returns the map with the keys in kmap renamed to the vals in kmap

Added by boxie

Returns the map with the vals mapped to the keys.

Added by boxie

disj[oin]. Returns a new set of the same (hashed/sorted) type, that does not contain key(s).

Added by boxie

Returns a lazy sequence of the elements of coll with duplicates removed. Returns a stateful transd...

Added by wdkrnls
4 Notes
    By , created 13.6 years ago

    The documentation doesn't mention the order, is it undefined?

    {:a :b :c :d}

    -> #{:d :a :b :c}

    By , created 8.5 years ago

    Sets are a core data structure that by definition do not store elements in a sorted order. The actual order will ultimately be determined by the specific hashing function underpinning the data structure.

    By , created 5.6 years ago

    Is the order guaranteed to be consistent if so is it not ordered (just not asc/desc)?

    By , created 5.4 years ago

    Sets are not ordered, so there’s not guarantee on their “order”.