Reduces an expression across an array a, using an index named idx,
and return value named ret, initialized to init, setting ret to the
evaluation of expr at each step, returning ret.
;; This should be about as quick as summing up a array of floats in java.
user=> (defn asum [#^floats xs]
(areduce xs i ret (float 0)
(+ ret (aget xs i))))
user=> (asum (float-array [1 2 3]))
6.0
(defmacro areduce
"Reduces an expression across an array a, using an index named idx,
and return value named ret, initialized to init, setting ret to the
evaluation of expr at each step, returning ret."
{:added "1.0"}
[a idx ret init expr]
`(let [a# ~a]
(loop [~idx (int 0) ~ret ~init]
(if (< ~idx (alength a#))
(recur (unchecked-inc ~idx) ~expr)
~ret))))
Comments top
No comments for areduce. Log in to add a comment.