(think)

An online novel about the Source, the Force, the real life and everything in between...

Drop-nth in Clojure

For some reason the standard Clojure library doesn’t have a drop-nth function (although it has take-nth). Luckily implementing it is trivial:

1
2
3
4
5
6
(defn drop-nth
  [n coll]
  (->> coll
       (map vector (iterate inc 1))
       (remove #(zero? (mod (first %) n)))
       (map second)))

Let’s try it out:

1
2
3
4
5
6
(drop-nth 3 (range 1 10))
;; => (1 2 4 5 7 8)
(drop-nth 5 (range 1 10))
;; => (1 2 3 4 6 7 8 9)
(drop-nth 5 (range 1 20))
;; => (1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19)

Looks good to me. Hopefully it will be of some use to someone.

Comments