当我开启devtools时偶尔会看到请求中的这个键,但并非总是如此.
我有以下功能:
(defun inc-map ()
(let ((ht #s(hash-table test contents-hash)))
(dolist (i (list 1 2 3 4))
(let ((old-val (or (gethash "foo" ht)
0)))
(puthash "foo" (+ 1 old-val) ht)))
ht))
Run Code Online (Sandbox Code Playgroud)
尽管此函数似乎在ht本地定义了该符号,但该函数似乎并不是引用透明的.特别是,调用它一次返回哈希表"foo" -> 4,第二次调用返回"foo" -> 8,第三次返回"foo" -> 12,依此类推.
这里到底发生了什么,如何将此功能更改为引用透明(并且"foo" -> 4每次都返回)?
我在调整一些性能敏感代码时遇到了这个问题:
user> (use 'criterium.core)
nil
user> (def n (into {} (for [i (range 20000) :let [k (keyword (str i))]] [k {k k}])))
#'user/n
user> (quick-bench (-> n :1 :1))
WARNING: Final GC required 32.5115186521176 % of runtime
Evaluation count : 15509754 in 6 samples of 2584959 calls.
Execution time mean : 36.256135 ns
Execution time std-deviation : 1.076403 ns
Execution time lower quantile : 35.120871 ns ( 2.5%)
Execution time upper quantile : 37.470993 ns (97.5%)
Overhead used : 1.755171 …Run Code Online (Sandbox Code Playgroud)