Skip to content

Instantly share code, notes, and snippets.

@ddeaguiar
Created October 7, 2012 23:18
Show Gist options
  • Save ddeaguiar/3849937 to your computer and use it in GitHub Desktop.
Save ddeaguiar/3849937 to your computer and use it in GitHub Desktop.
My alternative solutions for 4clojure #41
(defn f [col pos]
(loop [c col cnt 1 acc []]
(if (empty? c)
acc
(recur (rest c) (inc cnt) (if (= 0 (mod cnt pos))
acc (conj acc (first c)))))))
(= (f [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
(= (f [:a :b :c :d :e :f] 2) [:a :c :e])
(= (f [1 2 3 4 5 6] 4) [1 2 3 5 6])
(defn f2 [col pos]
(flatten (map #(if (= pos (count %))
(drop-last %) %)
(partition-all pos col))))
(= (f2 [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
(= (f2 [:a :b :c :d :e :f] 2) [:a :c :e])
(= (f2 [1 2 3 4 5 6] 4) [1 2 3 5 6])
@ddeaguiar
Copy link
Author

Some solutions used keep-index but ones I found the most interesting used partition-all. My initial attempt used neither.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment