• (last coll)
Return the last item in coll, in linear time

2 Examples top

  • user=> (last [1 2 3 4 5])
    5
    user=> (last ["a" "b" "c" "d" "e"])
    "e"
    user=> (last {:one 1 :two 2 :three 3})
    [:three 3]
    user=> (last [])
    nil
    
  • ;really slow reverse
    ;put the last item of the list at the start of a new list, and recur over all but the last item of the list.
    ;butlast acts similar to next in that it returns null for a 1-item list.
    
    (defn my-reverse
      ([a-list]
         (cond (= a-list nil) nil
               :else (cons (last a-list)
                           (my-reverse (butlast a-list))))))
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:242 top

(def 
 ^{:arglists '([coll])
   :doc "Return the last item in coll, in linear time"
   :added "1.0"
   :static true}
 last (fn ^:static last [s]
        (if (next s)
          (recur (next s))
          (first s))))
Vars in clojure.core/last:
Used in 0 other vars

Comments top

No comments for last. Log in to add a comment.