take-nth - Clojure Standard Library
For a quick intro to this series of blog posts check out Clojure Standard Library - Intro. It includes a lot of useful info, including notes about presentation of examples and more.
This posts function:
clojure.core/take-nth is a function that accepts a collection and returns every nth item in that collection.
In this example I am going to use
take-nth to get every third number from a collection that contains numbers 0 up to 20 (exclusive). The collection of numbers will be generated using
(take-nth 3 (range 20)) ; => (0 3 6 9 12 15 18) ; Every third number from range(20).
How To Use
Parameters and Return Values
take-nth is a multiple arity function and uses arity overloading in order to provide different behavior based on the number of arguments you provide.
take-nth takes one to two arguments.
(take-nth n)Providing only
n, returns a stateful transducer.
(take-nth n coll)Providing both
nand a collection (
coll), returns a lazy sequence that contains every nth item from
Note: Non-positive integers and zero return an infinite lazy sequence of zeros. 1
Example Use Cases
One possible use might be to use take-nth to do a quick, naive downsample of a stream of data. If in the stream, items close together are likely to contain similar information or, if it is already random.
(def random-stream (repeatedly #(rand-int 20))) ; Created an infinite stream of random integers ; => #'user/random-stream (def sample (take-nth 20 random-stream)) ; Created a sample of random-stream; contains every 20th integer. ; => #'user/sample (take 10 sample) ; => (19 14 5 3 12 10 13 15 2 14) ; The first ten items from sample.