Skip to content

Instantly share code, notes, and snippets.

@kindlychung kindlychung/ar2.clj
Last active Aug 29, 2015

What would you like to do?
(defmacro areduce2 [f init xs]
`(let [xs# ~xs]
(loop [idx# 0 ret# ~init]
(if (< idx# (alength xs#))
(recur (unchecked-inc idx#) (~f ret# (aget xs# idx#)))
(areduce2 + 0 (int-array (range 100)))
(defn areduce1 [f init xs]
(loop [idx 0 ret init]
(if (< idx (alength xs))
(recur (unchecked-inc idx) (f init (aget xs idx)))
(areduce1 + 0 (int-array (range 100)))
(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 0 ~ret ~init] (if (< ~idx (alength a#))
(recur (unchecked-inc ~idx) ~expr) ~ret))))

This comment has been minimized.

Copy link
Owner Author

commented May 7, 2015

(areduce1 + 0 (int-array (range 100)))
=> 99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.