From 7c521f1e042a723be225457fa2b5e42f3a681ada Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 18 Oct 2017 19:00:09 -0400 Subject: - Fixed a bug when converting an array into a list (also improved the performance). --- stdlib/source/lux/data/coll/array.lux | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'stdlib/source') 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 Eq) (All [a] (-> (Eq a) (Eq (Array a)))) -- cgit v1.2.3