【问题标题】:hash function behaves different with empty vector in clojure and clojurescript散列函数的行为与 clojure 和 clojurescript 中的空向量不同
【发布时间】:2015-01-06 01:05:53
【问题描述】:

这些天,我试图利用 clojure/clojurescript 中的函数哈希来生成唯一的 id,但事实证明,这个函数对于从 cljs 中的 read-string 和 clj 中解析的空向量有非常奇怪的行为。

在 clojure 中,散列函数返回 -2017569654 为空向量和从 read-string 解析的向量

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

然而,在 cljs 中,(hash []) 出人意料地返回 0。而 [] 也等于 (read-string "[]")

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

cljs.user=> (hash [])
0

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

有没有人知道是什么原因以及如何在cljs中解决?

【问题讨论】:

    标签: hash clojure clojurescript


    【解决方案1】:

    我怀疑这是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"
    

    与其他类型相比,[] 的散列看起来有点不对劲。

    【讨论】:

    • 谢谢,你是对的!在修复这个错误之前,我似乎必须考虑其他方式。
    • ClojureScript 的发布周期相当快,应该不会太长。
    • 好像有补丁,所以应该很快就会解决。
    • 已修复。它将在下一个版本中发布。
    猜你喜欢
    • 1970-01-01
    • 2013-09-27
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 2013-12-28
    相关资源
    最近更新 更多