Created
July 13, 2013 04:06
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see, PersistentHashSet uses
IEquiv
's-equiv
to test for duplicates.