liveBook · Manning

Hash-unordered-coll

As explained in Clojure: The Essential Reference

hash-unordered-coll is a Clojure function that computes a collection hash independent of the order of its elements.

hash-unordered-coll computes a collection hash without depending on element order. Use it when equivalent unordered collections must produce the same hash, including collections used as associative keys. section 6.6

Listing 6.6. → Hashing function
(hash [x])
(hash-ordered-coll [coll])
(hash-unordered-coll [coll])
(mix-collection-hash [hash-basis count])

The related hashing API lists hash-unordered-coll alongside hash-ordered-coll and mix-collection-hash. section 6.6

Order does not affect the result

(= (hash-unordered-coll [1 2 3]) (hash-unordered-coll [3 2 1]) (hash-unordered-coll #{1 2 3})) ;; true By contrast, hash-ordered-coll distinguishes these orderings. section 6.6

The function’s contract is compact:

  • Accepts one coll argument implementing java.lang.Iterable. section 6.6
  • Returns a java.lang.Long in the signed 32-bit integer range. section 6.6
  • Throws NullPointerException for nil and ClassCastException for non-Iterable values. section 6.6

How Clojure: The Essential Reference covers it

How deep it goes
Defines input, output, exceptions, and ordering behavior, but does not detail the internal mixing algorithm.
Examples
Demonstrates equal hashes for reordered vectors and sets, then contrasts the result with ordered collection hashing.
What it assumes
Assumes familiarity with Clojure collections, hashing, equality, and the java.lang.Iterable interface.
What you can do after
Apply it to normalize hashes across equivalent unordered collections before updating or looking up associative data.
How it connects
Connects hash-unordered-coll with hash, hash-ordered-coll, mix-collection-hash, Java hashing, and Iterable.

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.