mirror of
https://github.com/janet-lang/janet
synced 2024-12-23 23:10:26 +00:00
Update take/drop - while/until.
to be more consistent with take/drop
This commit is contained in:
parent
ee8a68f7b2
commit
5802155882
@ -704,20 +704,22 @@
|
|||||||
|
|
||||||
(defn take
|
(defn take
|
||||||
"Take first n elements in an indexed type. Returns new indexed instance."
|
"Take first n elements in an indexed type. Returns new indexed instance."
|
||||||
[n xs]
|
[n ind]
|
||||||
(def use-str (bytes? xs))
|
(def use-str (bytes? ind))
|
||||||
(def f (if use-str string/slice tuple/slice))
|
(def f (if use-str string/slice tuple/slice))
|
||||||
# make sure end is in [0, len]
|
# make sure end is in [0, len]
|
||||||
(def end (max 0 (min n (length xs))))
|
(def end (max 0 (min n (length ind))))
|
||||||
(f xs 0 end))
|
(f ind 0 end))
|
||||||
|
|
||||||
(defn take-until
|
(defn take-until
|
||||||
"Same as (take-while (complement pred) ind)."
|
"Same as (take-while (complement pred) ind)."
|
||||||
[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))
|
(def i (find-index pred ind))
|
||||||
(if i
|
(def end (if (nil? i) len i))
|
||||||
(array/slice ind 0 i)
|
(f ind 0 end))
|
||||||
ind))
|
|
||||||
|
|
||||||
(defn take-while
|
(defn take-while
|
||||||
"Given a predicate, take only elements from an indexed type that satisfy
|
"Given a predicate, take only elements from an indexed type that satisfy
|
||||||
@ -727,20 +729,22 @@
|
|||||||
|
|
||||||
(defn drop
|
(defn drop
|
||||||
"Drop first n elements in an indexed type. Returns new indexed instance."
|
"Drop first n elements in an indexed type. Returns new indexed instance."
|
||||||
[n xs]
|
[n ind]
|
||||||
(def use-str (bytes? xs))
|
(def use-str (bytes? ind))
|
||||||
(def f (if use-str string/slice tuple/slice))
|
(def f (if use-str string/slice tuple/slice))
|
||||||
# make sure start is in [0, len]
|
# make sure start is in [0, len]
|
||||||
(def start (max 0 (min n (length xs))))
|
(def start (max 0 (min n (length ind))))
|
||||||
(f xs start -1))
|
(f ind start -1))
|
||||||
|
|
||||||
(defn drop-until
|
(defn drop-until
|
||||||
"Same as (drop-while (complement pred) ind)."
|
"Same as (drop-while (complement pred) ind)."
|
||||||
[pred ind]
|
[pred ind]
|
||||||
|
(def use-str (bytes? ind))
|
||||||
|
(def f (if use-str string/slice tuple/slice))
|
||||||
(def i (find-index pred ind))
|
(def i (find-index pred ind))
|
||||||
(if i
|
(def len (length ind))
|
||||||
(array/slice ind i)
|
(def start (if (nil? i) len i))
|
||||||
@[]))
|
(f ind start))
|
||||||
|
|
||||||
(defn drop-while
|
(defn drop-while
|
||||||
"Given a predicate, remove elements from an indexed type that satisfy
|
"Given a predicate, remove elements from an indexed type that satisfy
|
||||||
|
@ -91,19 +91,20 @@
|
|||||||
|
|
||||||
# take-until
|
# take-until
|
||||||
|
|
||||||
(assert (deep= (take-until pos? @[]) @[]) "take-until 1")
|
(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]) []) "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 -3]) "take-until 3")
|
||||||
(assert (deep= (take-until pos? @[-1 -2 3]) @[-1 -2]) "take-until 4")
|
(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? @[-1 1 -2]) [-1]) "take-until 5")
|
||||||
|
(assert (deep= (take-until |(= $ 115) "books") "book") "take-until 6")
|
||||||
|
|
||||||
# take-while
|
# take-while
|
||||||
|
|
||||||
(assert (deep= (take-while neg? @[]) @[]) "take-while 1")
|
(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]) []) "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 -3]) "take-while 3")
|
||||||
(assert (deep= (take-while neg? @[-1 -2 3]) @[-1 -2]) "take-while 4")
|
(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? @[-1 1 -2]) [-1]) "take-while 5")
|
||||||
|
|
||||||
# drop
|
# drop
|
||||||
|
|
||||||
@ -116,11 +117,12 @@
|
|||||||
|
|
||||||
# drop-until
|
# drop-until
|
||||||
|
|
||||||
(assert (deep= (drop-until pos? @[]) @[]) "drop-until 1")
|
(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]) [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]) []) "drop-until 3")
|
||||||
(assert (deep= (drop-until pos? @[-1 -2 3]) @[3]) "drop-until 4")
|
(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? @[-1 1 -2]) [1 -2]) "drop-until 5")
|
||||||
|
(assert (deep= (drop-until |(= $ 115) "books") "s") "drop-until 6")
|
||||||
|
|
||||||
# Quasiquote bracketed tuples
|
# Quasiquote bracketed tuples
|
||||||
(assert (= (tuple/type ~[1 2 3]) (tuple/type '[1 2 3])) "quasiquote bracket tuples")
|
(assert (= (tuple/type ~[1 2 3]) (tuple/type '[1 2 3])) "quasiquote bracket tuples")
|
||||||
|
Loading…
Reference in New Issue
Block a user