Last active
August 29, 2015 14:20
-
-
Save kindlychung/cd025ba6b79848cb2168 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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#))) | |
ret#)))) | |
(areduce2 + 0 (int-array (range 100))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn areduce1 [f init xs] | |
(loop [idx 0 ret init] | |
(if (< idx (alength xs)) | |
(recur (unchecked-inc idx) (f init (aget xs idx))) | |
ret))) | |
(areduce1 + 0 (int-array (range 100))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(areduce1 + 0 (int-array (range 100)))
=> 99