为了建立一个数据结构,我发现自己做了很多事情,比如:
(let [foo (atom [])]
(do
(swap! foo conj {:foo "bar"})
(swap! foo conj {:foo "baz"}))
@foo)
=> [{:foo "bar"} {:foo "baz"}]
Run Code Online (Sandbox Code Playgroud)
这是一种反模式吗?我使用了很多原子。
我预计有 100 个密钥对,但实际只有 6 个。为什么?
(defn random-map-generator [size]
(def theatom (atom {}))
(dotimes [x size]
(swap! theatom assoc
(keyword (nth ["a" "b" "c" "d" "e" "f"] (rand-int 6)))
(rand-int 30)))
@theatom)
(random-map-generator 100)
=> {:f 4, :a 29, :e 15, :c 23, :b 19, :d 28}
Run Code Online (Sandbox Code Playgroud) 如何在集合上循环但从该集合中的不同指定点开始?也就是说,如果我有 [“a” “b” “c” “d” “e”] 能够通过指定从第三个位置开始(或者可能是第二个,如果它需要被零索引)?