1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-24 23:40:27 +00:00

update keys, values, pairs

This commit is contained in:
primo-ppcg 2023-09-01 13:18:31 +07:00
parent f969fb69e1
commit 645109048b

View File

@ -1586,32 +1586,41 @@
(defn keys (defn keys
"Get the keys of an associative data structure." "Get the keys of an associative data structure."
[x] [x]
(def arr @[]) (if (lengthable? x)
(var i 0) (do
(eachk k x (def arr (array/new-filled (length x)))
(put arr i k) (var i 0)
(++ i)) (eachk k x
arr) (put arr i k)
(++ i))
arr)
(seq [k :keys x] k)))
(defn values (defn values
"Get the values of an associative data structure." "Get the values of an associative data structure."
[x] [x]
(def arr @[]) (if (lengthable? x)
(var i 0) (do
(each v x (def arr (array/new-filled (length x)))
(put arr i v) (var i 0)
(++ i)) (each v x
arr) (put arr i v)
(++ i))
arr)
(seq [v :in x] v)))
(defn pairs (defn pairs
"Get the key-value pairs of an associative data structure." "Get the key-value pairs of an associative data structure."
[x] [x]
(def arr @[]) (if (lengthable? x)
(var i 0) (do
(eachp p x (def arr (array/new-filled (length x)))
(put arr i p) (var i 0)
(++ i)) (eachp p x
arr) (put arr i p)
(++ i))
arr)
(seq [p :pairs x] p)))
(defn frequencies (defn frequencies
"Get the number of occurrences of each value in an indexed data structure." "Get the number of occurrences of each value in an indexed data structure."