1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-16 02:09:56 +00:00

Merge pull request #1241 from primo-ppcg/keys-values-pairs

Rework `keys`, `values`, `pairs`
This commit is contained in:
Calvin Rose 2023-08-06 08:10:43 -05:00 committed by GitHub
commit 06eea74b98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1566,31 +1566,31 @@
(defn keys
"Get the keys of an associative data structure."
[x]
(def arr @[])
(var k (next x nil))
(while (not= nil k)
(array/push arr k)
(set k (next x k)))
(def arr (array/new-filled (length x)))
(var i 0)
(eachk k x
(put arr i k)
(++ i))
arr)
(defn values
"Get the values of an associative data structure."
[x]
(def arr @[])
(var k (next x nil))
(while (not= nil k)
(array/push arr (in x k))
(set k (next x k)))
(def arr (array/new-filled (length x)))
(var i 0)
(each v x
(put arr i v)
(++ i))
arr)
(defn pairs
"Get the key-value pairs of an associative data structure."
[x]
(def arr @[])
(var k (next x nil))
(while (not= nil k)
(array/push arr (tuple k (in x k)))
(set k (next x k)))
(def arr (array/new-filled (length x)))
(var i 0)
(eachp p x
(put arr i p)
(++ i))
arr)
(defn frequencies