liveBook · Manning

Hash-set

As explained in Clojure: The Essential Reference

hash-set is a Clojure function that creates an unsorted set from zero or more values, retaining only one value for each equality class.

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

How Clojure: The Essential Reference covers it

How deep it goes
Explains inputs, outputs, equality, metadata, literals, and implementation performance, while deferring detailed set algorithms.
Examples
Uses duplicate keywords, evaluated random values, metadata, membership predicates, and collection conversion examples.
What it assumes
Assumes familiarity with Clojure collections and equality while defining set uniqueness and empty-constructor behavior.
What you can do after
Apply it via Clojure to construct unique sets, evaluate dynamic elements, and perform concise membership tests.
How it connects
Connects hash-set with sets, set literals, set, sorted-set, contains?, some, conj, into, and clojure.set operations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.