1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-23 15:00: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
"Get the keys of an associative data structure."
[x]
(def arr @[])
(var i 0)
(eachk k x
(put arr i k)
(++ i))
arr)
(if (lengthable? x)
(do
(def arr (array/new-filled (length x)))
(var i 0)
(eachk k x
(put arr i k)
(++ i))
arr)
(seq [k :keys x] k)))
(defn values
"Get the values of an associative data structure."
[x]
(def arr @[])
(var i 0)
(each v x
(put arr i v)
(++ i))
arr)
(if (lengthable? x)
(do
(def arr (array/new-filled (length x)))
(var i 0)
(each v x
(put arr i v)
(++ i))
arr)
(seq [v :in x] v)))
(defn pairs
"Get the key-value pairs of an associative data structure."
[x]
(def arr @[])
(var i 0)
(eachp p x
(put arr i p)
(++ i))
arr)
(if (lengthable? x)
(do
(def arr (array/new-filled (length x)))
(var i 0)
(eachp p x
(put arr i p)
(++ i))
arr)
(seq [p :pairs x] p)))
(defn frequencies
"Get the number of occurrences of each value in an indexed data structure."