1
0
mirror of https://github.com/janet-lang/janet synced 2025-05-21 08:44:13 +00:00

Update take/drop implementation.

No more preserves input type.
This commit is contained in:
curist 2019-08-06 14:19:22 +08:00
parent 18da183ef7
commit 61bbeebfba

View File

@ -705,15 +705,10 @@
(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 xs]
(def [f empty] (case (type xs) (def use-str (bytes? xs))
:array [array/slice @[]] (def f (if use-str string/slice tuple/slice))
:buffer [buffer/slice @""] # make sure end in [0, len]
:string [string/slice ""] (def end (max 0 (min n (length xs))))
:tarray [tarray/slice @[]]
:tuple [tuple/slice (if (= :parens (tuple/type xs)) '() [])]
(error "should provide an indexed type")))
(def len (if (= :tarray (type xs)) (tarray/length xs) (length xs)))
(def end (if (pos? n) (min n len) 0))
(f xs 0 end)) (f xs 0 end))
(defn take-until (defn take-until
@ -733,15 +728,10 @@
(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 xs]
(def [f empty] (case (type xs) (def use-str (bytes? xs))
:array [array/slice @[]] (def f (if use-str string/slice tuple/slice))
:buffer [buffer/slice @""] # make sure start in [0, len]
:string [string/slice ""] (def start (max 0 (min n (length xs))))
:tarray [tarray/slice @[]]
:tuple [tuple/slice (if (= :parens (tuple/type xs)) '() [])]
(error "should provide an indexed type")))
(def len (if (= :tarray (type xs)) (tarray/length xs) (length xs)))
(def start (if (pos? n) (min n len) 0))
(f xs start -1)) (f xs start -1))
(defn drop-until (defn drop-until