diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/data/coll/array.lux | 65 |
1 files changed, 29 insertions, 36 deletions
diff --git a/stdlib/source/lux/data/coll/array.lux b/stdlib/source/lux/data/coll/array.lux index 750e6b610..833da6230 100644 --- a/stdlib/source/lux/data/coll/array.lux +++ b/stdlib/source/lux/data/coll/array.lux @@ -1,19 +1,13 @@ (;module: lux (lux (control monoid - ["F" functor] + [functor #+ Functor] [eq #+ Eq] fold) - (data (coll [list "List/" Fold<List>]) + (data (coll [list "list/" Fold<List>]) [product]) )) -## [Types] -(type: #export (Array a) - {#;doc "Mutable arrays."} - (#;Host "#Array" (#;Cons a #;Nil))) - -## [Functions] (def: #export (new size) (All [a] (-> Nat (Array a))) (_lux_proc ["array" "new"] [size])) @@ -22,17 +16,17 @@ (All [a] (-> (Array a) Nat)) (_lux_proc ["array" "size"] [xs])) -(def: #export (get i xs) +(def: #export (read i xs) (All [a] (-> Nat (Array a) (Maybe a))) (_lux_proc ["array" "get"] [xs i])) -(def: #export (put i x xs) +(def: #export (write i x xs) (All [a] (-> Nat a (Array a) (Array a))) (_lux_proc ["array" "put"] [xs i x])) -(def: #export (remove i xs) +(def: #export (delete i xs) (All [a] (-> Nat (Array a) (Array a))) (_lux_proc ["array" "remove"] [xs i])) @@ -42,21 +36,21 @@ (Array a))) (if (n.= +0 length) dest-array - (List/fold (function [offset target] - (case (get (n.+ offset src-start) src-array) + (list/fold (function [offset target] + (case (read (n.+ offset src-start) src-array) #;None target (#;Some value) - (put (n.+ offset dest-start) value target))) + (write (n.+ offset dest-start) value target))) dest-array (list;n.range +0 (n.dec length))))) (def: #export (occupied array) {#;doc "Finds out how many cells in an array are occupied."} (All [a] (-> (Array a) Nat)) - (List/fold (function [idx count] - (case (get idx array) + (list/fold (function [idx count] + (case (read idx array) #;None count @@ -73,16 +67,16 @@ (def: #export (filter p xs) (All [a] (-> (-> a Bool) (Array a) (Array a))) - (List/fold (: (-> Nat (Array ($ +0)) (Array ($ +0))) + (list/fold (: (-> Nat (Array ($ +0)) (Array ($ +0))) (function [idx xs'] - (case (get idx xs) + (case (read idx xs) #;None xs' (#;Some x) (if (p x) xs' - (remove idx xs'))))) + (delete idx xs'))))) xs (list;indices (size xs)))) @@ -92,7 +86,7 @@ (let [arr-size (size xs)] (loop [idx +0] (if (n.< arr-size idx) - (case (get idx xs) + (case (read idx xs) #;None (recur (n.inc idx)) @@ -109,7 +103,7 @@ (let [arr-size (size xs)] (loop [idx +0] (if (n.< arr-size idx) - (case (get idx xs) + (case (read idx xs) #;None (recur (n.inc idx)) @@ -122,28 +116,28 @@ (def: #export (clone xs) (All [a] (-> (Array a) (Array a))) (let [arr-size (size xs)] - (List/fold (function [idx ys] - (case (get idx xs) + (list/fold (function [idx ys] + (case (read idx xs) #;None ys (#;Some x) - (put idx x ys))) + (write idx x ys))) (new arr-size) (list;indices arr-size)))) (def: #export (from-list xs) (All [a] (-> (List a) (Array a))) - (product;right (List/fold (function [x [idx arr]] - [(n.inc idx) (put idx x arr)]) + (product;right (list/fold (function [x [idx arr]] + [(n.inc idx) (write idx x arr)]) [+0 (new (list;size xs))] xs))) (def: #export (to-list array) (All [a] (-> (Array a) (List a))) (let [_size (size array)] - (product;right (List/fold (function [_ [idx tail]] - (case (get idx array) + (product;right (list/fold (function [_ [idx tail]] + (case (read idx array) (#;Some head) [(n.dec idx) (#;Cons head tail)] @@ -153,16 +147,15 @@ (list;repeat _size []) )))) -## [Structures] (struct: #export (Eq<Array> Eq<a>) (All [a] (-> (Eq a) (Eq (Array a)))) (def: (= xs ys) (let [sxs (size xs) sxy (size ys)] (and (n.= sxy sxs) - (List/fold (function [idx prev] + (list/fold (function [idx prev] (and prev - (case [(get idx xs) (get idx ys)] + (case [(read idx xs) (read idx ys)] [#;None #;None] true @@ -186,19 +179,19 @@ (copy sxs +0 xs +0) (copy sxy +0 ys sxs))))) -(struct: #export _ (F;Functor Array) +(struct: #export _ (Functor Array) (def: (map f ma) (let [arr-size (size ma)] (if (n.= +0 arr-size) (new arr-size) - (List/fold (: (-> Nat (Array ($ +1)) (Array ($ +1))) + (list/fold (: (-> Nat (Array ($ +1)) (Array ($ +1))) (function [idx mb] - (case (get idx ma) + (case (read idx ma) #;None mb (#;Some x) - (put idx (f x) mb)))) + (write idx (f x) mb)))) (new arr-size) (list;n.range +0 (n.dec arr-size))))))) @@ -208,7 +201,7 @@ (loop [so-far init idx +0] (if (n.< arr-size idx) - (case (get idx xs) + (case (read idx xs) #;None (recur so-far (n.inc idx)) |