aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEduardo Julian2017-10-18 19:00:09 -0400
committerEduardo Julian2017-10-18 19:00:09 -0400
commit7c521f1e042a723be225457fa2b5e42f3a681ada (patch)
tree9c5cd746f395f9a8c0c695207d1a75c0fa3d6c64
parentab24a9e2cededf2c59b2b0c336f00629ba7f2ccd (diff)
- Fixed a bug when converting an array into a list (also improved the performance).
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/data/coll/array.lux24
1 files changed, 13 insertions, 11 deletions
diff --git a/stdlib/source/lux/data/coll/array.lux b/stdlib/source/lux/data/coll/array.lux
index 833da6230..a4a3aa903 100644
--- a/stdlib/source/lux/data/coll/array.lux
+++ b/stdlib/source/lux/data/coll/array.lux
@@ -133,19 +133,21 @@
[+0 (new (list;size xs))]
xs)))
+(def: underflow Nat (n.dec +0))
+
(def: #export (to-list array)
(All [a] (-> (Array a) (List a)))
- (let [_size (size array)]
- (product;right (list/fold (function [_ [idx tail]]
- (case (read idx array)
- (#;Some head)
- [(n.dec idx) (#;Cons head tail)]
-
- #;None
- [(n.dec idx) tail]))
- [(n.dec _size) #;Nil]
- (list;repeat _size [])
- ))))
+ (loop [idx (n.dec (size array))
+ output #;Nil]
+ (if (n.= underflow idx)
+ output
+ (recur (n.dec idx)
+ (case (read idx array)
+ (#;Some head)
+ (#;Cons head output)
+
+ #;None
+ output)))))
(struct: #export (Eq<Array> Eq<a>)
(All [a] (-> (Eq a) (Eq (Array a))))