blob: a8f8d9f00463a27ea0ea94205c6a7015db027655 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
(;module:
lux
(lux (control monoid
functor
applicative
monad
eq
fold)
(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]))
(def: #export (size xs)
(All [a] (-> (Array a) Nat))
(_lux_proc ["array" "size"] [xs]))
(def: #export (get i xs)
(All [a]
(-> Nat (Array a) (Maybe a)))
(_lux_proc ["array" "get"] [xs i]))
(def: #export (put i x xs)
(All [a]
(-> Nat a (Array a) (Array a)))
(_lux_proc ["array" "put"] [xs i x]))
(def: #export (remove i xs)
(All [a]
(-> Nat (Array a) (Array a)))
(_lux_proc ["array" "remove"] [xs i]))
(def: #export (copy length src-start src-array dest-start dest-array)
(All [a] (-> Nat Nat (Array a) Nat (Array a)
(Array a)))
(if (n.= +0 length)
dest-array
(List/fold (function [offset target]
(case (get (n.+ offset src-start) src-array)
#;None
target
(#;Some value)
(put (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)
#;None
count
(#;Some _)
(n.inc count)))
+0
(list;indices (size array))))
(def: #export (vacant array)
{#;doc "Finds out how many cells in an array are vacant."}
(All [a] (-> (Array a) Nat))
(n.- (occupied array) (size array)))
(def: #export (filter p xs)
(All [a]
(-> (-> a Bool) (Array a) (Array a)))
(List/fold (: (-> Nat (Array ($ +0)) (Array ($ +0)))
(function [idx xs']
(case (get idx xs)
#;None
xs'
(#;Some x)
(if (p x)
xs'
(remove idx xs')))))
xs
(list;indices (size xs))))
(def: #export (find p xs)
(All [a]
(-> (-> a Bool) (Array a) (Maybe a)))
(let [arr-size (size xs)]
(loop [idx +0]
(if (n.< arr-size idx)
(case (get idx xs)
#;None
(recur (n.inc idx))
(#;Some x)
(if (p x)
(#;Some x)
(recur (n.inc idx))))
#;None))))
(def: #export (find+ p xs)
{#;doc "Just like 'find', but with access to the index of each value."}
(All [a]
(-> (-> Nat a Bool) (Array a) (Maybe [Nat a])))
(let [arr-size (size xs)]
(loop [idx +0]
(if (n.< arr-size idx)
(case (get idx xs)
#;None
(recur (n.inc idx))
(#;Some x)
(if (p idx x)
(#;Some [idx x])
(recur (n.inc idx))))
#;None))))
(def: #export (clone xs)
(All [a] (-> (Array a) (Array a)))
(let [arr-size (size xs)]
(List/fold (function [idx ys]
(case (get idx xs)
#;None
ys
(#;Some x)
(put 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)])
[+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)
(#;Some head)
[(n.dec idx) (#;Cons head tail)]
#;None
[(n.dec idx) tail]))
[(n.dec _size) #;Nil]
(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]
(and prev
(case [(get idx xs) (get idx ys)]
[#;None #;None]
true
[(#;Some x) (#;Some y)]
(:: Eq<a> = x y)
_
false)))
true
(list;n.range +0 (n.dec sxs)))))
))
(struct: #export Monoid<Array> (All [a]
(Monoid (Array a)))
(def: unit (new +0))
(def: (append xs ys)
(let [sxs (size xs)
sxy (size ys)]
(|> (new (n.+ sxy sxs))
(copy sxs +0 xs +0)
(copy sxy +0 ys sxs)))))
(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)))
(function [idx mb]
(case (get idx ma)
#;None
mb
(#;Some x)
(put idx (f x) mb))))
(new arr-size)
(list;n.range +0 (n.dec arr-size)))))))
(struct: #export _ (Fold Array)
(def: (fold f init xs)
(let [arr-size (size xs)]
(loop [so-far init
idx +0]
(if (n.< arr-size idx)
(case (get idx xs)
#;None
(recur so-far (n.inc idx))
(#;Some value)
(recur (f value so-far) (n.inc idx)))
so-far)))))
|