1
0
mirror of https://github.com/janet-lang/janet synced 2025-02-23 11:40:01 +00:00

take-drop dictionaries

Return table for `take` of dictionary types.
Allow `drop` of dictionary types.
This commit is contained in:
primo-ppcg 2023-06-07 19:20:05 +07:00
parent 472ec730b5
commit b5407ac708
2 changed files with 29 additions and 0 deletions

View File

@ -1095,6 +1095,9 @@
(cond (cond
(bytes? ind) (take-n-slice string/slice n ind) (bytes? ind) (take-n-slice string/slice n ind)
(indexed? ind) (take-n-slice tuple/slice n ind) (indexed? ind) (take-n-slice tuple/slice n ind)
(dictionary? ind) (do
(var left n)
(tabseq [[i x] :pairs ind :until (< (-- left) 0)] i x))
(do (do
(def res @[]) (def res @[])
(var key nil) (var key nil)
@ -1116,6 +1119,7 @@
(cond (cond
(bytes? ind) (take-until-slice string/slice pred ind) (bytes? ind) (take-until-slice string/slice pred ind)
(indexed? ind) (take-until-slice tuple/slice pred ind) (indexed? ind) (take-until-slice tuple/slice pred ind)
(dictionary? ind) (tabseq [[i x] :pairs ind :until (pred x)] i x)
(seq [x :in ind :until (pred x)] x))) (seq [x :in ind :until (pred x)] x)))
(defn take-while (defn take-while
@ -1132,6 +1136,13 @@
(def end (if negn (max 0 (+ len n)) len)) (def end (if negn (max 0 (+ len n)) len))
(f ind start end)) (f ind start end))
(defn- drop-n-dict
[f n ind]
(def res (f ind))
(var left n)
(loop [[i x] :pairs ind :until (< (-- left) 0)] (set (res i) nil))
res)
(defn drop (defn drop
``Drop the first `n` elements in an indexed or bytes type. Returns a new tuple or string ``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.`` instance, respectively. If `n` is negative, drops the last `n` elements instead.``
@ -1139,6 +1150,8 @@
(cond (cond
(bytes? ind) (drop-n-slice string/slice n ind) (bytes? ind) (drop-n-slice string/slice n ind)
(indexed? ind) (drop-n-slice tuple/slice n ind) (indexed? ind) (drop-n-slice tuple/slice n ind)
(struct? ind) (drop-n-dict struct/to-table n ind)
(table? ind) (drop-n-dict table/clone n ind)
(do (do
(var key nil) (var key nil)
(repeat n (repeat n
@ -1152,12 +1165,20 @@
(def start (if (nil? i) len i)) (def start (if (nil? i) len i))
(f ind start)) (f ind start))
(defn- drop-until-dict
[f pred ind]
(def res (f ind))
(loop [[i x] :pairs ind :until (pred x)] (set (res i) nil))
res)
(defn drop-until (defn drop-until
"Same as `(drop-while (complement pred) ind)`." "Same as `(drop-while (complement pred) ind)`."
[pred ind] [pred ind]
(cond (cond
(bytes? ind) (drop-until-slice string/slice pred ind) (bytes? ind) (drop-until-slice string/slice pred ind)
(indexed? ind) (drop-until-slice tuple/slice pred ind) (indexed? ind) (drop-until-slice tuple/slice pred ind)
(struct? ind) (drop-until-dict struct/to-table pred ind)
(table? ind) (drop-until-dict table/clone pred ind)
(do (find pred ind) ind))) (do (find pred ind) ind)))
(defn drop-while (defn drop-while

View File

@ -513,6 +513,14 @@
(def sqr2 (drop 10 (squares))) (def sqr2 (drop 10 (squares)))
(assert (deep= (take 1 sqr2) @[100]) "drop fiber next value") (assert (deep= (take 1 sqr2) @[100]) "drop fiber next value")
(def dict @{:a 1 :b 2 :c 3 :d 4 :e 5})
(def dict1 (take 2 dict))
(def dict2 (drop 2 dict))
(assert (= (length dict1) 2) "take dictionary")
(assert (= (length dict2) 3) "drop dictionary")
(assert (deep= (merge dict1 dict2) dict) "take-drop symmetry for dictionary")
# Comment macro # Comment macro
# issue #110 - 698e89aba # issue #110 - 698e89aba
(comment 1) (comment 1)