nth

functionsince v0.0-927 clojure.core/nthEdit
(nth coll n)
(nth coll n not-found)

Details:

Returns the value at index n or not-found if the index is out of bounds.

nth will throw an exception if n is out of bounds and not-found is not supplied.

nth works for Strings, Arrays, Regex Matchers, Lists, and Sequences. For Sequences, nth takes O(n) time.


See Also:


Source docstring:
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 code @ clojurescript:src/main/cljs/cljs/core.cljs
(defn nth
  ([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 (< -1 n (.-length coll)))
        (aget coll (int n))
        (throw (js/Error. "Index out of bounds")))

      (string? coll)
      (if (and (< -1 n (.-length coll)))
        (.charAt coll (int n))
        (throw (js/Error. "Index out of bounds")))

      (or (implements? ISeq coll)
          (implements? ISequential coll))
      (if (neg? n)
        (throw (js/Error. "Index out of bounds"))
        (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 (< -1 n (.-length coll)))
        (aget coll (int n))
        not-found)

      (string? coll)
      (if (and (< -1 n (.-length coll)))
        (.charAt coll (int n))
        not-found)

      (or (implements? ISeq coll)
          (implements? ISequential coll))
      (if (neg? n)
        not-found
        (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))))))))