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.
(def an-array (int-array 25000 (int 0)))
user=> (time (amap ^ints an-array
idx
ret
(+ (int 1)
(aget ^ints an-array idx))))
"Elapsed time: 14.708653 msecs"
;; Note: without type hinting the performance of would not be good.
(defmacro amap
"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."
{:added "1.0"}
[a idx ret expr]
`(let [a# ~a
~ret (aclone a#)]
(loop [~idx 0]
(if (< ~idx (alength a#))
(do
(aset ~ret ~idx ~expr)
(recur (unchecked-inc ~idx)))
~ret))))
Comments top
No comments for amap. Log in to add a comment.