Hen*_*gon 7 functional-programming clojure
将函数映射到序列中每个第n个元素的简洁方法是什么?类似的东西(map-every-nth fn coll n),它将返回原始序列,只有每个第n个元素被转换,例如(map-every-nth inc(范围16)4)将返回(0 1 2 4 4 5 6 8 8 9 10 12 12 13 14 16)
Ósc*_*pez 11
试试这个:
(defn map-every-nth [f coll n]
(map-indexed #(if (zero? (mod (inc %1) n)) (f %2) %2) coll))
(map-every-nth inc (range 16) 4)
> (0 1 2 4 4 5 6 8 8 9 10 12 12 13 14 16)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2417 次 |
| 最近记录: |