aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/collection/array.lux
blob: 630b8351fe6a84cdcfed9de1bf54affa0bb9f934 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
(.module:
  [lux #*
   ["@" target]
   [abstract
    [monoid (#+ Monoid)]
    [functor (#+ Functor)]
    [equivalence (#+ Equivalence)]
    fold
    [predicate (#+ Predicate)]]
   [data
    ["." product]
    ["." maybe]
    [number
     ["n" nat]]
    [collection
     ["." list ("#\." fold)]]]])

(def: #export type-name "#Array")

(type: #export (Array a)
  {#.doc "Mutable arrays."}
  (#.Primitive ..type-name (#.Cons a #.Nil)))

(with-expansions [<index-type> (primitive "java.lang.Long")
                  <elem-type> (primitive "java.lang.Object")
                  <array-type> (type (Array <elem-type>))]
  (for {@.jvm
        (template: (!int value)
          (|> value
              (:coerce <index-type>)
              "jvm object cast"
              "jvm conversion long-to-int"))}
       (as-is))
  
  (def: #export (new size)
    (All [a] (-> Nat (Array a)))
    (for {@.old
          (:assume ("jvm anewarray" "(java.lang.Object )" size))

          @.jvm
          (|> size
              !int
              "jvm array new object"
              (: <array-type>)
              :assume)

          @.js
          ("js array new" size)}))

  (def: #export (size array)
    (All [a] (-> (Array a) Nat))
    (for {@.old
          ("jvm arraylength" array)

          @.jvm
          (|> array
              (:coerce <array-type>)
              "jvm array length object"
              "jvm conversion int-to-long"
              "jvm object cast"
              (: <index-type>)
              (:coerce Nat))

          @.js
          ("js array length" array)}))

  (def: #export (read index array)
    (All [a]
      (-> Nat (Array a) (Maybe a)))
    (if (n.< (size array) index)
      (for {@.old
            (let [value ("jvm aaload" array index)]
              (if ("jvm object null?" value)
                #.None
                (#.Some value)))

            @.jvm
            (let [value (|> array
                            (:coerce <array-type>)
                            ("jvm array read object" (!int index)))]
              (if ("jvm object null?" value)
                #.None
                (#.Some (:assume value))))

            @.js
            (let [output ("js array read" index array)]
              (if ("js object undefined?" output)
                #.None
                (#.Some output)))})
      #.None))

  (def: #export (write! index value array)
    (All [a]
      (-> Nat a (Array a) (Array a)))
    (for {@.old
          ("jvm aastore" array index value)

          @.jvm
          (|> array
              (:coerce <array-type>)
              ("jvm array write object" (!int index) (:coerce <elem-type> value))
              :assume)

          @.js
          ("js array write" index value array)}))

  (def: #export (delete! index array)
    (All [a]
      (-> Nat (Array a) (Array a)))
    (if (n.< (size array) index)
      (for {@.old
            (write! index (:assume ("jvm object null")) array)

            @.jvm
            (write! index (:assume (: <elem-type> ("jvm object null"))) array)

            @.js
            ("js array delete" index array)})
      array))
  )

(def: #export (contains? index array)
  (All [a]
    (-> Nat (Array a) Bit))
  (case (..read index array)
    (#.Some _)
    true

    _
    false))

(def: #export (update! index transform array)
  (All [a]
    (-> Nat (-> a a) (Array a) (Array a)))
  (case (read index array)
    #.None
    array

    (#.Some value)
    (write! index (transform value) array)))

(def: #export (upsert! index default transform array)
  (All [a]
    (-> Nat a (-> a a) (Array a) (Array a)))
  (write! index
          (|> array (read index) (maybe.default default) transform)
          array))

(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 (read (n.+ offset src-start) src-array)
                   #.None
                   target
                   
                   (#.Some value)
                   (write! (n.+ offset dest-start) value target)))
               dest-array
               (list.indices length))))

(def: #export (occupancy array)
  {#.doc "Finds out how many cells in an array are occupied."}
  (All [a] (-> (Array a) Nat))
  (list\fold (function (_ idx count)
               (case (read idx array)
                 #.None
                 count
                 
                 (#.Some _)
                 (inc count)))
             0
             (list.indices (size array))))

(def: #export (vacancy array)
  {#.doc "Finds out how many cells in an array are vacant."}
  (All [a] (-> (Array a) Nat))
  (n.- (..occupancy array) (..size array)))

(def: #export (filter! p xs)
  (All [a]
    (-> (Predicate a) (Array a) (Array a)))
  (list\fold (function (_ idx xs')
               (case (read idx xs)
                 #.None
                 xs'

                 (#.Some x)
                 (if (p x)
                   xs'
                   (delete! idx xs'))))
             xs
             (list.indices (size xs))))

(def: #export (find p xs)
  (All [a]
    (-> (Predicate a) (Array a) (Maybe a)))
  (let [arr-size (size xs)]
    (loop [idx 0]
      (if (n.< arr-size idx)
        (case (read idx xs)
          #.None
          (recur (inc idx))
          
          (#.Some x)
          (if (p x)
            (#.Some x)
            (recur (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 Bit) (Array a) (Maybe [Nat a])))
  (let [arr-size (size xs)]
    (loop [idx 0]
      (if (n.< arr-size idx)
        (case (read idx xs)
          #.None
          (recur (inc idx))
          
          (#.Some x)
          (if (p idx x)
            (#.Some [idx x])
            (recur (inc idx))))
        #.None))))

(def: #export (clone xs)
  (All [a] (-> (Array a) (Array a)))
  (let [arr-size (size xs)]
    (list\fold (function (_ idx ys)
                 (case (read idx xs)
                   #.None
                   ys

                   (#.Some x)
                   (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])
                              [(inc idx) (write! idx x arr)])
                            [0 (new (list.size xs))]
                            xs)))

(def: underflow Nat (dec 0))

(def: #export (to-list array)
  (All [a] (-> (Array a) (List a)))
  (loop [idx (dec (size array))
         output #.Nil]
    (if (n.= ..underflow idx)
      output
      (recur (dec idx)
             (case (read idx array)
               (#.Some head)
               (#.Cons head output)

               #.None
               output)))))

(def: #export (to-list' default array)
  (All [a] (-> a (Array a) (List a)))
  (loop [idx (dec (size array))
         output #.Nil]
    (if (n.= ..underflow idx)
      output
      (recur (dec idx)
             (#.Cons (maybe.default default (read idx array))
                     output)))))

(structure: #export (equivalence (^open ",\."))
  (All [a] (-> (Equivalence a) (Equivalence (Array a))))
  
  (def: (= xs ys)
    (let [sxs (size xs)
          sxy (size ys)]
      (and (n.= sxy sxs)
           (list\fold (function (_ idx prev)
                        (and prev
                             (case [(read idx xs) (read idx ys)]
                               [#.None #.None]
                               true

                               [(#.Some x) (#.Some y)]
                               (,\= x y)

                               _
                               false)))
                      true
                      (list.indices sxs))))))

(structure: #export monoid
  (All [a] (Monoid (Array a)))
  
  (def: identity (new 0))

  (def: (compose xs ys)
    (let [sxs (size xs)
          sxy (size ys)]
      (|> (new (n.+ sxy sxs))
          (copy! sxs 0 xs 0)
          (copy! sxy 0 ys sxs)))))

(structure: #export functor
  (Functor Array)
  
  (def: (map f ma)
    (let [arr-size (size ma)]
      (if (n.= 0 arr-size)
        (new arr-size)
        (list\fold (function (_ idx mb)
                     (case (read idx ma)
                       #.None
                       mb

                       (#.Some x)
                       (write! idx (f x) mb)))
                   (new arr-size)
                   (list.indices arr-size))
        ))))

(structure: #export fold
  (Fold Array)
  
  (def: (fold f init xs)
    (let [arr-size (size xs)]
      (loop [so-far init
             idx 0]
        (if (n.< arr-size idx)
          (case (read idx xs)
            #.None
            (recur so-far (inc idx))

            (#.Some value)
            (recur (f value so-far) (inc idx)))
          so-far)))))

(template [<name> <init> <op>]
  [(def: #export (<name> predicate array)
     (All [a]
       (-> (Predicate a) (Predicate (Array a))))
     (let [size (..size array)]
       (loop [idx 0]
         (if (n.< size idx)
           (case (..read idx array)
             (#.Some value)
             (<op> (predicate value)
                   (recur (inc idx)))
             
             #.None
             (recur (inc idx)))
           <init>))))]

  [every? true  and]
  [any?   false or]
  )