Skip to content

Instantly share code, notes, and snippets.

@luxbock luxbock/navs.clj
Created Mar 21, 2017

Embed
What would you like to do?
(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
You can’t perform that action at this time.