1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-23 15:00:27 +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

View File

@ -91,19 +91,20 @@
# take-until
(assert (deep= (take-until pos? @[]) @[]) "take-until 1")
(assert (deep= (take-until pos? @[1 2 3]) @[]) "take-until 2")
(assert (deep= (take-until pos? @[-1 -2 -3]) @[-1 -2 -3]) "take-until 3")
(assert (deep= (take-until pos? @[-1 -2 3]) @[-1 -2]) "take-until 4")
(assert (deep= (take-until pos? @[-1 1 -2]) @[-1]) "take-until 5")
(assert (deep= (take-until pos? @[]) []) "take-until 1")
(assert (deep= (take-until pos? @[1 2 3]) []) "take-until 2")
(assert (deep= (take-until pos? @[-1 -2 -3]) [-1 -2 -3]) "take-until 3")
(assert (deep= (take-until pos? @[-1 -2 3]) [-1 -2]) "take-until 4")
(assert (deep= (take-until pos? @[-1 1 -2]) [-1]) "take-until 5")
(assert (deep= (take-until |(= $ 115) "books") "book") "take-until 6")
# take-while
(assert (deep= (take-while neg? @[]) @[]) "take-while 1")
(assert (deep= (take-while neg? @[1 2 3]) @[]) "take-while 2")
(assert (deep= (take-while neg? @[-1 -2 -3]) @[-1 -2 -3]) "take-while 3")
(assert (deep= (take-while neg? @[-1 -2 3]) @[-1 -2]) "take-while 4")
(assert (deep= (take-while neg? @[-1 1 -2]) @[-1]) "take-while 5")
(assert (deep= (take-while neg? @[]) []) "take-while 1")
(assert (deep= (take-while neg? @[1 2 3]) []) "take-while 2")
(assert (deep= (take-while neg? @[-1 -2 -3]) [-1 -2 -3]) "take-while 3")
(assert (deep= (take-while neg? @[-1 -2 3]) [-1 -2]) "take-while 4")
(assert (deep= (take-while neg? @[-1 1 -2]) [-1]) "take-while 5")
# drop
@ -116,11 +117,12 @@
# drop-until
(assert (deep= (drop-until pos? @[]) @[]) "drop-until 1")
(assert (deep= (drop-until pos? @[1 2 3]) @[1 2 3]) "drop-until 2")
(assert (deep= (drop-until pos? @[-1 -2 -3]) @[]) "drop-until 3")
(assert (deep= (drop-until pos? @[-1 -2 3]) @[3]) "drop-until 4")
(assert (deep= (drop-until pos? @[-1 1 -2]) @[1 -2]) "drop-until 5")
(assert (deep= (drop-until pos? @[]) []) "drop-until 1")
(assert (deep= (drop-until pos? @[1 2 3]) [1 2 3]) "drop-until 2")
(assert (deep= (drop-until pos? @[-1 -2 -3]) []) "drop-until 3")
(assert (deep= (drop-until pos? @[-1 -2 3]) [3]) "drop-until 4")
(assert (deep= (drop-until pos? @[-1 1 -2]) [1 -2]) "drop-until 5")
(assert (deep= (drop-until |(= $ 115) "books") "s") "drop-until 6")
# Quasiquote bracketed tuples
(assert (= (tuple/type ~[1 2 3]) (tuple/type '[1 2 3])) "quasiquote bracket tuples")