Fork me on GitHub
#clojurescript
<
2019-12-27
>
hmaurer12:12:27

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?

hmaurer12:12:28

cljs.user=> (hash 1)
1
cljs.user=> (hash true)
1
on http://clojurescript.net/

hmaurer12:12:41

this doesn’t even seem to match the definition of hash given at https://cljs.github.io/api/cljs.core/hash

hmaurer12:12:02

(cond ...
  (true? o) 1231
  ...
)

p-himik13:12:48

> 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. >

p-himik13:12:25

> Woudn’t that break hashmaps, etc? No, because data structures that deal with hashes know about such hash problems and work around them.

p-himik13:12:27

> 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.

hmaurer13:12:27

@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

hmaurer13:12:32

Thanks for the answer on the other points 🙂

hmaurer13:12:51

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?

hmaurer13:12:48

@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)”

p-himik13:12:50

Yes. In fact, I think that's how some languages/libraries compare e.g. strings. Don't quote me on that though. :)

hmaurer13:12:02

Cool; ty! :)

👍 4
Ian M18:12:57

Is there a predicate I can use with spec to validate that a value is a JS object? This is the best thing I can come up with:

(s/def ::attributes #(= (type #js {}) (type %)))

p-himik18:12:42

You can replace (type #js {}) with js/Object.

Ian M18:12:48

that makes tons of sense, thank you

p-himik18:12:59

And you probably should replace = with identical?.

p-himik18:12:22

Oh wait. There's actually a predicate for that - object?. :D

👍 8
Ian M18:12:31

don’t know why i couldn’t find that one :face_palm:

p-himik18:12:29

CLJS sources are split between CLJS and CLJC files - I often can't find stuff right away because of that.