1
0
mirror of https://github.com/janet-lang/janet synced 2025-11-04 17:43:02 +00:00

drop with a negative count now drops from the end

This commit is contained in:
Ian Henry
2023-04-23 21:35:15 -07:00
parent d9ed7a77f8
commit 54b54f85f3
2 changed files with 14 additions and 8 deletions

View File

@@ -1133,16 +1133,17 @@
(take-until (complement pred) ind))
(defn drop
``Drop the first n elements in an indexed or bytes type. Returns a new tuple or string
instance, respectively.``
``Drop the first `n elements in an indexed or bytes type. Returns a new tuple or string
instance, respectively. If `n` is negative, drops the last `n` elements instead.``
[n ind]
(def use-str (bytes? ind))
(def f (if use-str string/slice tuple/slice))
(def len (length ind))
# make sure start is in [0, len]
(def m (if (> n 0) n 0))
(def start (if (> m len) len m))
(f ind start -1))
(def [start end]
(if (>= n 0)
[(min n len) len]
[0 (max 0 (+ len n))]))
(f ind start end))
(defn drop-until
"Same as `(drop-while (complement pred) ind)`."