macro | since v0.0-927 | clojure.core/amap | Edit |
(amap a idx ret expr)
For quickly creating a new JavaScript array by mapping an expression expr
across a JavaScript array a
. The expression can use ret
as the current
result, which is initialized to a
. It can also use idx
to get the current
index.
(def a #js [1 2 3])
(amap a i ret (* 10 (aget a i)))
;;=> #js [10 20 30]
You can also use ret
inside the mapped expression if you want to use the
current result:
(def a #js [1 2 3])
(amap a i ret (+ (if (pos? i)
(aget ret (dec i))
0)
(* 10 (aget a i))))
;;=> #js [10 30 60]
Maps an expression across an array a, using an index named idx, and return value named ret, initialized to a clone of a, then setting each element of ret to the evaluation of expr, returning the new array ret.
(core/defmacro amap
[a idx ret expr]
`(let [a# ~a
l# (alength a#)
~ret (cljs.core/aclone a#)]
(loop [~idx 0]
(if (< ~idx l#)
(do
(aset ~ret ~idx ~expr)
(recur (inc ~idx)))
~ret))))