1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-24 15:30: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)
(do
(def arr (array/new-filled (length x)))
(var i 0) (var i 0)
(eachk k x (eachk k x
(put arr i k) (put arr i k)
(++ i)) (++ i))
arr) 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)
(do
(def arr (array/new-filled (length x)))
(var i 0) (var i 0)
(each v x (each v x
(put arr i v) (put arr i v)
(++ i)) (++ i))
arr) 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)
(do
(def arr (array/new-filled (length x)))
(var i 0) (var i 0)
(eachp p x (eachp p x
(put arr i p) (put arr i p)
(++ i)) (++ i))
arr) 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."