【发布时间】:2014-03-09 19:35:58
【问题描述】:
我需要从integers 的vector 中获取一个随机值。
我还需要取回没有值键的vector。
示例代码如下。我知道我可以轻松地将这段代码放在function 中以重复使用。
但是想知道是否有一些功能或更好的方法可以在没有第 n 个元素的情况下创建一个新的vector? (因此是核心 clojure 中 (nth) function 的补码)
(let [ vector [1 2 3 5 9 1]
id (rand-int (count vector))
value (nth vector id)
head (take id vector)
tail (drop (+ id 1) vector)
vector (flatten [head tail])]
{:value value :new-vector vector})
【问题讨论】: