1.3.0 permalink Arrow_down_16x16

butlast

clojure.core

  • (butlast coll)
Return a seq of all but the last item in coll, in linear time

2 Examples top

  • user=> (butlast [1 2 3])
    (1 2)
    user=> (butlast (butlast [1 2 3]))
    (1)
    user=> (butlast (butlast (butlast [1 2 3])))
    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:252 top

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

Comments top

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