Problem
Implement take-nth
.
Solutions
Solution 1
(defn my-take-nth [n col]
(loop [i 0
result []]
(if (= i (count col))
result
(if (= 0 (mod i n))
(recur (inc i) (conj result (nth col i)))
(recur (inc i) result)))))
solution 2
(defn my-take-nth2 [n col]
(->> col
(map-indexed (fn [i x] [i x]))
(filter (fn [[i x]] (= 0 (mod i 2))))
(mapv (fn [[i x]] x))))
Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Email