From 7417e82c51afc20f5d68756dd865cd2b73c2751d Mon Sep 17 00:00:00 2001 From: primo-ppcg Date: Sun, 6 Aug 2023 15:35:13 +0700 Subject: [PATCH] rework `keys`, `values`, `pairs` --- src/boot/boot.janet | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 46432619..0f59e5e1 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -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