Skip to content

Instantly share code, notes, and snippets.

@luxbock
Created March 21, 2017 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luxbock/69b766830144e4bdb767923ba7fb2f9c to your computer and use it in GitHub Desktop.
Save luxbock/69b766830144e4bdb767923ba7fb2f9c to your computer and use it in GitHub Desktop.
(defnav drop-nav [n]
(select* [this structure next-fn]
(next-fn
(if (vector? structure)
(subvec structure (min n (count structure)))
(drop n structure))))
(transform* [this structure next-fn]
(if (vector? structure)
(let [mi (min n (count structure))]
(into (subvec structure 0 mi)
(next-fn (subvec structure mi (count structure)))))
(concat (take n structure)
(next-fn (drop n structure))))))
(defnav take-nav [n]
(select* [this structure next-fn]
(next-fn
(if (vector? structure)
(subvec structure 0 (min n (count structure)))
(take n structure))))
(transform* [this structure next-fn]
(if (vector? structure)
(let [mi (min n (count structure))]
(into (next-fn (subvec structure 0 mi))
(subvec structure mi (count structure))))
(concat (next-fn (take n structure))
(drop n structure)))))
(defnav REST []
(select* [this structure next-fn]
(next-fn
(if (vector? structure)
(subvec structure 1)
(rest structure))))
(transform* [this structure next-fn]
(if (vector? structure)
(into [(first structure)]
(next-fn (subvec structure 1)))
(concat (take 1 structure)
(next-fn (rest structure))))))
(defnav every-nth [n]
(select* [this structure next-fn]
(next-fn (take-nth n structure)))
(transform* [this structure next-fn]
(if (vector? structure)
(let [c (count structure)
is (into []
(take-while #(< % c))
(iterate #(+ % n) 0))]
(reduce #(update %1 %2 next-fn) structure is))
(sequence
(map-indexed
(fn [i x]
(if (zero? (mod i n))
(next-fn x)
x)))
structure))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment