假设有一个名字向量:
(def noms ["Tim" "Joseph" "Steven" "Michael"])
如何从奇数索引中获取名称?
更新:
实际上,我试图在Clojure中重写此Rust代码:
fn main() {
let nums = [1, 2];
let noms = ["Tim", "Eston", "Aaron", "Ben"];
let mut odds = nums.iter().map(|&x| x * 2 - 1);
for num in odds {
spawn(proc() {
println!("{:s} says hello from a lightweight thread!", noms[num]);
});
}
}
有惯用的方法吗?
编辑:
这是与上述Rust代码几乎相同的Clojure代码:
(def noms ["Tim", "Eston", "Aaron", "Ben"])
(doseq [i (take-nth 2 (rest noms))]
(println i "says hello from a lightweight thread!"))
新的问题是:如何编写“轻量级”线程(或Clojure术语中的等效项)?
最佳答案
您可以使用take-nth
(take-nth 2 (rest ["Tim" "Joseph" "Steven" "Michael"]))