Skip to content

clojure.core/nth

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 2 Runnable: 1
([coll index] [coll index not-found])

Returns the value at the index. get returns nil if index out of bounds, nth throws an exception unless not-found is supplied. nth also works for strings, Java arrays, regex Matchers and Lists, and, in O(n) time, for sequences.

Examples

by MrHus on . May have evaluation errors.
by st-keller on

Note by pesterhazy

Rather than throwing an exception, (nth nil n) returns nil for any number n.

Note by beoliver

Floats are treated as indexes (nth [0 1] 1.9) returns the element at index 1. At first glance it may seem as though they are always rounded down. However due to floating point precision (nth [0 1] 1.9999999999999999) will raise an IndexOutOfBoundsException.

See also


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