(nth coll n)
(nth coll n 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, arrays, regex Matchers and Lists, and,
in O(n) time, for sequences.
Source
(defn
nth
"Returns the value at the index. get returns nil if index out of\n bounds, nth throws an exception unless not-found is supplied. nth\n also works for strings, arrays, regex Matchers and Lists, and,\n in O(n) time, for sequences."
([coll n]
(cond
(not (number? n))
(throw (js/Error. "Index argument to nth must be a number"))
(nil? coll)
coll
(implements? IIndexed coll)
(-nth coll n)
(array? coll)
(if
(and (>= n 0) (< n (.-length coll)))
(aget coll n)
(throw (js/Error. "Index out of bounds")))
(string? coll)
(if
(and (>= n 0) (< n (.-length coll)))
(.charAt coll n)
(throw (js/Error. "Index out of bounds")))
(implements? ISeq coll)
(linear-traversal-nth coll n)
(native-satisfies? IIndexed coll)
(-nth coll n)
:else
(throw
(js/Error.
(str
"nth not supported on this type "
(type->str (type coll)))))))
([coll n not-found]
(cond
(not (number? n))
(throw (js/Error. "Index argument to nth must be a number."))
(nil? coll)
not-found
(implements? IIndexed coll)
(-nth coll n not-found)
(array? coll)
(if (and (>= n 0) (< n (.-length coll))) (aget coll n) not-found)
(string? coll)
(if (and (>= n 0) (< n (.-length coll))) (.charAt coll n) not-found)
(implements? ISeq coll)
(linear-traversal-nth coll n not-found)
(native-satisfies? IIndexed coll)
(-nth coll n not-found)
:else
(throw
(js/Error.
(str
"nth not supported on this type "
(type->str (type coll))))))))