1
votes

These days, I was trying to utilize function hash in clojure/clojurescript to generate unique id, but it turns out this function has very strange behaviour to empty vector parsed from read-string in cljs with in clj.

In clojure, hash function return -2017569654 for empty vector and the one parsed from read-string

user=> (hash [])
-2017569654
user=> (hash (read-string "[]"))
-2017569654

However, in cljs, (hash []) returns surprisingly 0. And [] are also equal to (read-string "[]").

cljs.user=> (hash (cljs.reader/read-string "[]"))
-2017569654

cljs.user=> (hash [])
0

cljs.user=> (= [] (cljs.reader/read-string "[]"))
true

Does any one know the reason of it and how to solve it in cljs?

1

1 Answers

1
votes

I suspect this is a bug in ClojureScript.

homepage.core> [(hash [2]) (hash (cljs.reader/read-string "[2]"))]
[-1917711765 -1917711765]
homepage.core> [(hash [0]) (hash (cljs.reader/read-string "[0]"))]
[965192274 965192274]
homepage.core> [(hash []) (hash (cljs.reader/read-string "[]"))]
[0 -2017569654]
homepage.core> [(hash {}) (hash (cljs.reader/read-string "{}"))]
[-15128758 -15128758]
homepage.core> [(hash #{}) (hash (cljs.reader/read-string "#{}"))]
[0 0]
homepage.core> *clojurescript-version*
"0.0-2496"

When compared to the other types, the hashing of [] looks a bit off.