Skip to content

clojure.core/take-nth

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 2 Runnable: 2
([n] [n coll])

Returns a lazy seq of every nth item in coll. Returns a stateful transducer when no collection is provided.

Examples

by devn on
by miner on

Note by Dimagog

(take-nth 0 (range 10)) will loop forever.

Note by miner

(take-nth 0 coll) will return an infinite sequence repeating for first item from coll. A negative N is treated the same as 0.

Note by reborg

;; In case you are searching for it, drop-nth is not in core.

(defn drop-nth [n coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (concat (take (dec n) (rest s))
              (drop-nth n (drop n s))))))

(drop-nth 3 (range 10))
;; (1 2 4 5 7 8)

;; or alternatively: (keep-indexed #(when-not (zero? (rem %1 n)) %2) coll)

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.