ClojureDocs

Nav

Namespaces

hash-ordered-coll

clojure.core

Available since 1.6 (source)
  • (hash-ordered-coll coll)
Returns the hash code, consistent with =, for an external ordered
 collection implementing Iterable.
 See http://clojure.org/data_structures#hash for full algorithms.
2 Examples
;;;; Clojure's (hash-ordered-coll) produces the same hash code regardless
;;;; of collection type when both of the following two conditions are met:
;;;;   (1) collections contain the same elements
;;;;   (2) collection elements are ordered the same when returned by .iterator()

(hash-ordered-coll [1 2])
;;=> 156247261
(hash-ordered-coll '(1 2))
;;=> 156247261
(hash-ordered-coll (sorted-set 1 2))
;;=> 156247261
(hash-ordered-coll (doto (new java.util.ArrayList) (.add 1) (.add 2)))
;;=> 156247261
(hash-ordered-coll (doto (new java.util.TreeSet) (.add 1) (.add 2)))
;;=> 156247261

(hash-ordered-coll [2 1])
;;=> -1994590503
(hash-ordered-coll '(2 1))
;;=> -1994590503
(hash-ordered-coll (sorted-set-by > 2 1))
;;=> -1994590503
(hash-ordered-coll (doto (new java.util.ArrayList) (.add 2) (.add 1)))
;;=> -1994590503
(hash-ordered-coll (doto (new java.util.TreeSet >) (.add 2) (.add 1)))
;;=> -1994590503

;;;; Notice that this differs from (hash) which 
;;;;   (1) doesn't rely on element order as returned by .iterator()
;;;;   (2) falls back on Java's .hashCode() for non-Clojure collections

(hash [1 2])
;;=> 156247261
(hash '(1 2))
;;=> 156247261
(hash (sorted-set 1 2))
;;=> 460223544
(hash (sorted-set-by > 2 1))
;;=> 460223544
(hash (doto (new java.util.ArrayList) (.add 1) (.add 2)))
;;=> 994
(hash (doto (new java.util.TreeSet) (.add 1) (.add 2)))
;;=> 3
;;;;
;;;; Only accepts implementations of java.lang.Iterable
;;;;

(hash-ordered-coll true)
;;=> ClassCastException java.lang.Boolean cannot be cast to java.lang.Iterable
(hash-ordered-coll 1)
;;=> ClassCastException java.lang.Long cannot be cast to java.lang.Iterable
(hash-ordered-coll \c)
;;=> ClassCastException java.lang.Character cannot be cast to java.lang.Iterable

;;;;
;;;; Being seqable is not sufficient!
;;;;

(hash-ordered-coll "12")
;;=> ClassCastException java.lang.String cannot be cast to java.lang.Iterable
(hash-ordered-coll (int-array [1 2]))
;;=> ClassCastException [I cannot be cast to java.lang.Iterable
(hash-ordered-coll nil)
;;=> NullPointerException
See Also

Returns the hash code, consistent with =, for an external unordered collection implementing Itera...

Added by svenschoenung

Returns the hash code of its argument. Note this is the hash code consistent with =, and thus is d...

Added by svenschoenung
0 Notes
No notes for hash-ordered-coll