clojure.core/nth¶
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¶
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¶
- clojure.core/first (2)
- clojure.core/second
- clojure.core/nthnext (2)
- clojure.core/get (8)
- clojure.core/take-nth (2)
- clojure.core/nthrest (4)
- clojure.core/rand-nth (0)
Content from the matching ClojureDocs page, with authors credited on each contribution.