This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-27
Channels
- # babashka (24)
- # beginners (169)
- # cider (3)
- # clara (2)
- # clojars (1)
- # clojure (198)
- # clojure-europe (2)
- # clojure-nl (6)
- # clojure-spec (29)
- # clojure-uk (21)
- # clojurescript (21)
- # clojurewerkz (1)
- # core-async (6)
- # cryogen (2)
- # datomic (4)
- # duct (1)
- # fulcro (26)
- # graalvm (1)
- # malli (1)
- # reitit (5)
- # spacemacs (21)
Hello! Quick q: why are some values in ClojureScript allowed to have the same hash? (e.g. 1
and true
). Woudn’t that break hashmaps, etc?
cljs.user=> (hash 1)
1
cljs.user=> (hash true)
1
on http://clojurescript.net/this doesn’t even seem to match the definition of hash
given at https://cljs.github.io/api/cljs.core/hash
> why are some values in ClojureScript allowed to have the same hash? Hash collisions happen all the time. It's impossible to have a completely inversible hash function. >
> Woudn’t that break hashmaps, etc? No, because data structures that deal with hashes know about such hash problems and work around them.
> this doesn’t even seem to match the definition of hash given at https://cljs.github.io/api/cljs.core/hash This is true, and I've seen such comments before. I have no explanation for this, however. Perhaps, people at #cljs-dev know more.
@p-himik I checked the github repo of http://clojurescript.net and http://clojurescript.io and they haven’t been updated in years; perhaps they are outdated
And so, what does the docstring for hash
mean by “consistent with equality”? Does it mean that if two values are equal then they have the same hash, but not necessarily that they’re equal if they have the same hash?
@p-himik so the hash can be used to implement a faster check for equality by value (if cached)? e.g. “if hashes are different then return false, else do a deeper comparison (since it might be a collision)”
Yes. In fact, I think that's how some languages/libraries compare e.g. strings. Don't quote me on that though. :)