假设有一个名称向量:
(def noms ["Tim" "Joseph" "Steven" "Michael"])
Run Code Online (Sandbox Code Playgroud)
如何从 odd-index 获取名称?
更新:实际上,我试图在 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]);
});
}
}
Run Code Online (Sandbox Code Playgroud)
有一个惯用的方法来做到这一点吗?
编辑:这是与上述 Rust 代码几乎相同的 Clojure 代码:
(def noms ["Tim", "Eston", "Aaron", "Ben"])
(doseq [i (take-nth 2 (rest noms))]
(println i "says hello from a lightweight thread!"))
Run Code Online (Sandbox Code Playgroud)
新问题是:如何编写“轻量级”线程(或 Clojure 术语中的等效内容)?
您可以使用take-nth
(take-nth 2 (rest ["Tim" "Joseph" "Steven" "Michael"]))
Run Code Online (Sandbox Code Playgroud)