From 388346020221a2082b901f5f796434161025bb96 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 11 Jan 2021 15:01:41 -0600 Subject: [PATCH] Remove length checks to a number of core functions. This lets them be more generic and implemented over a wider range of data types, such as fibers. --- src/boot/boot.janet | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 1481049c..d92caba6 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -112,7 +112,7 @@ (defn true? "Check if x is true." [x] (= x true)) (defn false? "Check if x is false." [x] (= x false)) (defn nil? "Check if x is nil." [x] (= x nil)) -(defn empty? "Check if xs is empty." [xs] (= (length xs) 0)) +(defn empty? "Check if xs is empty." [xs] (= nil (next xs nil))) # For macros, we define an imcomplete odd? function that will be overriden. (defn odd? [x] (= 1 (mod x 2))) @@ -849,7 +849,7 @@ Returns a new array.` [f init ind] (var res init) - (def ret (array/new (length ind))) + (def ret @[]) (each x ind (array/push ret (set res (f res x)))) ret) @@ -859,7 +859,7 @@ return value will be (length ind).` [f ind] (var k (next ind)) - (def ret (array/new (length ind))) + (def ret @[]) (if (= nil k) (break ret)) (var res (in ind k)) (array/push ret res) @@ -1177,8 +1177,7 @@ (reduce fop x forms)) (defn- walk-ind [f form] - (def len (length form)) - (def ret (array/new len)) + (def ret @[]) (each x form (array/push ret (f x))) ret) @@ -1431,7 +1430,7 @@ (defn keys "Get the keys of an associative data structure." [x] - (def arr (array/new (length x))) + (def arr @[]) (var k (next x nil)) (while (not= nil k) (array/push arr k) @@ -1441,7 +1440,7 @@ (defn values "Get the values of an associative data structure." [x] - (def arr (array/new (length x))) + (def arr @[]) (var k (next x nil)) (while (not= nil k) (array/push arr (in x k)) @@ -1451,7 +1450,7 @@ (defn pairs "Get the key-value pairs of an associative data structure." [x] - (def arr (array/new (length x))) + (def arr @[]) (var k (next x nil)) (while (not= nil k) (array/push arr (tuple k (in x k))) @@ -1507,7 +1506,7 @@ `Takes a table or struct and returns and array of key value pairs like @[k v k v ...]. Returns a new array.` [dict] - (def ret (array/new (* 2 (length dict)))) + (def ret @[]) (loop [k :keys dict] (array/push ret k (in dict k))) ret)