hash-set creates an unsorted Clojure set from zero or more values. The result keeps one value from each equality class and supports fast lookup, addition, and removal. section 13.0 section 13.1
Listing 13.1. → Data Structure, Sets, Constructor
(hash-set [& keys])
The function accepts any number of keys, including none, making its arity and constructor role explicit. section 13.1
Its contract is straightforward:
- Accepts any number of values of any type. section 13.1
- Returns an empty set with no arguments. section 13.1
- Removes duplicate values according to equality semantics. section 13.1
- Retains metadata from the first equal value when equal values carry different metadata. section 13.1
Constructing a set from evaluated values
(hash-set :yellow :red :green :green) ;; #{:yellow :green :red} (hash-set (rand) (rand) (rand)) ;; #{0.53148213003 0.7171734431 0.5055531620} Use hash-set when the values require evaluation. The #{} literal is concise for constants, but duplicate entries in that literal raise an exception rather than being removed.
Sets can be called as predicates, so a set works naturally with some for membership checks. For an existing collection, prefer (set coll) over (apply hash-set coll); set creates a set from one collection argument. section 13.0 section 13.2
