Skip to content

Instantly share code, notes, and snippets.

@hadronzoo
Created July 13, 2013 04:06
Show Gist options
  • Save hadronzoo/5989375 to your computer and use it in GitHub Desktop.
Save hadronzoo/5989375 to your computer and use it in GitHub Desktop.
Hashing for custom types such that they are properly detected as duplicates within ClojureScript's PersistentHashSets.
(deftype TestType [key]
IHash
(-hash [this]
(hash key)))
(def a (TestType. [1 2 3]))
(def b (TestType. [1 2 3]))
;; the resulting PersistentHashSet should have one item, but has two:
(.log js/console (pr-str (conj #{} a b)))
;; → #{#<[object Object]> #<[object Object]>}
(.log js/console (== (hash a) (hash b)))
;; → true
@hadronzoo
Copy link
Author

I see, PersistentHashSet uses IEquiv's -equiv to test for duplicates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment