Created
October 7, 2012 23:18
-
-
Save ddeaguiar/3849937 to your computer and use it in GitHub Desktop.
My alternative solutions for 4clojure #41
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 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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some solutions used keep-index but ones I found the most interesting used partition-all. My initial attempt used neither.