1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-29 14:47:42 +00:00

Update take/drop - while/until.

to be more consistent with take/drop
This commit is contained in:
curist
2019-08-06 15:33:55 +08:00
parent ee8a68f7b2
commit 5802155882
2 changed files with 35 additions and 29 deletions

View File

@@ -704,20 +704,22 @@
(defn take
"Take first n elements in an indexed type. Returns new indexed instance."
[n xs]
(def use-str (bytes? xs))
[n ind]
(def use-str (bytes? ind))
(def f (if use-str string/slice tuple/slice))
# make sure end is in [0, len]
(def end (max 0 (min n (length xs))))
(f xs 0 end))
(def end (max 0 (min n (length ind))))
(f ind 0 end))
(defn take-until
"Same as (take-while (complement pred) ind)."
[pred ind]
(def use-str (bytes? ind))
(def f (if use-str string/slice tuple/slice))
(def len (length ind))
(def i (find-index pred ind))
(if i
(array/slice ind 0 i)
ind))
(def end (if (nil? i) len i))
(f ind 0 end))
(defn take-while
"Given a predicate, take only elements from an indexed type that satisfy
@@ -727,20 +729,22 @@
(defn drop
"Drop first n elements in an indexed type. Returns new indexed instance."
[n xs]
(def use-str (bytes? xs))
[n ind]
(def use-str (bytes? ind))
(def f (if use-str string/slice tuple/slice))
# make sure start is in [0, len]
(def start (max 0 (min n (length xs))))
(f xs start -1))
(def start (max 0 (min n (length ind))))
(f ind start -1))
(defn drop-until
"Same as (drop-while (complement pred) ind)."
[pred ind]
(def use-str (bytes? ind))
(def f (if use-str string/slice tuple/slice))
(def i (find-index pred ind))
(if i
(array/slice ind i)
@[]))
(def len (length ind))
(def start (if (nil? i) len i))
(f ind start))
(defn drop-while
"Given a predicate, remove elements from an indexed type that satisfy