Skip to content

Instantly share code, notes, and snippets.

@cjmcgraw
Created May 16, 2015 21:28
Show Gist options
  • Save cjmcgraw/d81cb9e35db131df73d7 to your computer and use it in GitHub Desktop.
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.
(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