macro | since v0.0-927 | clojure.core/areduce | Edit |
(areduce a idx ret init expr)
For quickly reducing an expression expr
across a JavaScript array a
. The
expression can use ret
as the current result, which is initialized to init
.
It can also use idx
to get the current index.
(def a #js [1 2 3])
(areduce a i ret 0 (+ ret (aget a i)))
;;=> 6
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.
(core/defmacro areduce
[a idx ret init expr]
`(let [a# ~a
l# (alength a#)]
(loop [~idx 0 ~ret ~init]
(if (< ~idx l#)
(recur (inc ~idx) ~expr)
~ret))))