liveBook · Manning

Hash-ordered-coll

As explained in Clojure: The Essential Reference

hash-ordered-coll is a Clojure function that computes a hash for an iterable collection while preserving the significance of element order.

hash-ordered-coll computes a collection hash whose result depends on element order. It accepts one argument, coll, which must implement java.lang.Iterable, and returns a java.lang.Long. section 6.6

Use it when ordered collections need hashing compatible with Clojure’s collection-hashing support. Passing nil raises NullPointerException; passing a non-Iterable raises ClassCastException. section 6.6

Order changes the hash

(= (hash-ordered-coll [1 2 3]) (hash-ordered-coll [3 2 1]) (hash-ordered-coll #{1 2 3})) ;; false Unlike hash-unordered-coll, this function distinguishes element order. section 6.6

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

Compare the related hashing APIs and their argument shapes. section 6.6

How Clojure: The Essential Reference covers it

How deep it goes
Defines the input contract, return type, exceptions, and order sensitivity without detailing the hashing algorithm’s internals.
Examples
Demonstrates differing hashes for reordered vectors and sets using hash-ordered-coll alongside hash-unordered-coll.
What you can do after
Apply it to hash ordered iterable collections while accounting for order sensitivity and invalid-input exceptions.
How it connects
Connects hash-ordered-coll with hash, hash-unordered-coll, mix-collection-hash, and java.lang.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.