From bb8b9054221607c62689d9507a02f91355132d89 Mon Sep 17 00:00:00 2001 From: Andrey Listopadov Date: Thu, 25 Aug 2022 23:30:22 +0300 Subject: correct pop --- init.fnl | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'init.fnl') diff --git a/init.fnl b/init.fnl index 321bd12..dafba1b 100644 --- a/init.fnl +++ b/init.fnl @@ -560,6 +560,8 @@ Empty tables created with `vector` will pass the test: (fn [t] (let [len (- (length* t) 1) coll []] + (when (< len 0) + (error "can't pop empty vector" 2)) (for [i 1 len] (tset coll i (. t i))) (vec* (itable coll) len)))) @@ -1961,13 +1963,15 @@ specified `key` or `keys`." _ (error (.. "disj is not supported on " (class Set)) 2)))) (defn pop - "For a list or queue, returns a new list/queue without the first -item, for a vector, returns a new vector without the last item. If -the collection is empty, throws an exception. Note - not the same -as next/butlast." + "If `coll` is a list returns a new list without the first +item. If `coll` is a vector, returns a new vector without the last +item. If the collection is empty, raises an error. Not the same as +`next` or `butlast`." [coll] (match (getmetatable coll) - {:cljlib/type :seq} (drop 1 coll) + {:cljlib/type :seq} (match (seq coll) + s (drop 1 s) + _ (error "can't pop empty list" 2)) {:cljlib/pop f} (f coll) _ (error (.. "pop is not supported on " (class coll)) 2))) -- cgit v1.2.3