我编写gen-sim-map函数的方式出了点问题。它在一个帐号后停止,并在向量中的最后一个号码后停止。我想知道我做错了什么。我可以通过对函数应用repeat来解决问题,但这对我来说似乎不对。
这是数据和电话;函数defs低于该值。
(def acct-nums (gen-acct-nums 5))
#'ba1-app/acct-nums
ba1-app=> acct-nums
[10000 10001 10002 10003 10004 10005]
ba1-app=> (gen-sim-map acct-nums)
{10005 [\W 160.12]}
(defn random-trans
[]
(nth avail-trans (.nextInt random (count avail-trans))))
(defn random-amount
[]
(float (/ (.nextInt random (count (range 1 10000))) 25 )))
; Generate an account number. The range is arbitrary to make it look a real bank account #.
(defn gen-acct-nums [range-end-idx]
(vec (range 10000 (+ 10000 range-end-idx 1))))
(defn gen-sim-map [acct-nums]
(reduce
(fn [sim-map one-acct-num]
(let [trans (random-trans )
amt (random-amount )]
{ one-acct-num (vector trans amt) } ))
{}
acct-nums))
答案 0 :(得分:2)
我怀疑你的问题是你没有更换累加器。我希望看到像
这样的东西(defn gen-sim-map [acct-nums]
(reduce
(fn [sim-map one-acct-num]
(let [trans (random-trans )
amt (random-amount )]
(assoc sim-map one-acct-num (vector trans amt)) )) ; <---
{}
acct-nums))
或类似。