From 645109048babcdcbf8081a0e77a1f47bf595333d Mon Sep 17 00:00:00 2001 From: primo-ppcg Date: Fri, 1 Sep 2023 13:18:31 +0700 Subject: [PATCH] update `keys`, `values`, `pairs` --- src/boot/boot.janet | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 012c3fb4..6eb4a9f9 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -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."