Created
May 16, 2015 21:28
-
-
Save cjmcgraw/d81cb9e35db131df73d7 to your computer and use it in GitHub Desktop.
Clojure function to map a given function over a given dataset every nth interval.
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 every-nth [f data n & {:keys [n-times] :or {n-times -1}}] | |
"Applies the function to every nth element, leaving other elements unaltered" | |
(map-indexed #(if (and | |
(zero? (mod (Math/abs (+ %1 1)) n)) | |
(> n (quot %1 (* (Integer/signum n) n-times)))) | |
(f %2) %2) | |
data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment