aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/array.lux
blob: 73e8a209bcec261c4ea8d8f3f2d17d9b91e06560 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
(.module:
  [library
   [lux (#- list)
    ["@" target]
    [abstract
     [monoid (#+ Monoid)]
     [functor (#+ Functor)]
     [equivalence (#+ Equivalence)]
     [mix (#+ Mix)]
     [predicate (#+ Predicate)]]
    [control
     ["." maybe]]
    [data
     ["." product]
     [collection
      ["." list ("#\." mix)]]]
    [math
     [number
      ["n" nat]]]]])

(def: .public type_name
  "#Array")

(type: .public (Array a)
  (#.Primitive ..type_name (#.Item a #.End)))

(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
               (:as <index_type>)
               "jvm object cast"
               "jvm conversion long-to-int")])}
       (as_is))
  
  (def: .public (empty size)
    (All [a] (-> Nat (Array a)))
    (for {@.old
          (:expected ("jvm anewarray" "(java.lang.Object )" size))

          @.jvm
          (|> size
              !int
              "jvm array new object"
              (: <array_type>)
              :expected)

          @.js ("js array new" size)
          @.python ("python array new" size)
          @.lua ("lua array new" size)
          @.ruby ("ruby array new" size)
          @.php ("php array new" size)
          @.scheme ("scheme array new" size)}))

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

          @.jvm
          (|> array
              (:as <array_type>)
              "jvm array length object"
              "jvm conversion int-to-long"
              "jvm object cast"
              (: <index_type>)
              (:as Nat))

          @.js ("js array length" array)
          @.python ("python array length" array)
          @.lua ("lua array length" array)
          @.ruby ("ruby array length" array)
          @.php ("php array length" array)
          @.scheme ("scheme array length" array)}))

  (template: (!read! <read!> <null?>)
    [(let [output (<read!> index array)]
       (if (<null?> output)
         #.None
         (#.Some output)))])

  (def: .public (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
                            (:as <array_type>)
                            ("jvm array read object" (!int index)))]
              (if ("jvm object null?" value)
                #.None
                (#.Some (:expected value))))

            @.js (!read! "js array read" "js object undefined?")
            @.python (!read! "python array read" "python object none?")
            @.lua (!read! "lua array read" "lua object nil?")
            @.ruby (!read! "ruby array read" "ruby object nil?")
            @.php (!read! "php array read" "php object null?")
            @.scheme (!read! "scheme array read" "scheme object nil?")})
      #.None))

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

          @.jvm
          (|> array
              (:as <array_type>)
              ("jvm array write object" (!int index) (:as <elem_type> value))
              :expected)

          @.js ("js array write" index value array)
          @.python ("python array write" index value array)
          @.lua ("lua array write" index value array)
          @.ruby ("ruby array write" index value array)
          @.php ("php array write" index value array)
          @.scheme ("scheme array write" index value array)}))

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

            @.jvm
            (write! index (:expected (: <elem_type> ("jvm object null"))) array)

            @.js ("js array delete" index array)
            @.python ("python array delete" index array)
            @.lua ("lua array delete" index array)
            @.ruby ("ruby array delete" index array)
            @.php ("php array delete" index array)
            @.scheme ("scheme array delete" index array)})
      array))
  )

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

    _
    false))

(def: .public (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: .public (upsert! index default transform array)
  (All [a]
    (-> Nat a (-> a a) (Array a) (Array a)))
  (write! index
          (|> array (read! index) (maybe.else default) transform)
          array))

(def: .public (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\mix (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: .public (occupancy array)
  (All [a] (-> (Array a) Nat))
  (list\mix (function (_ idx count)
              (case (read! idx array)
                #.None
                count
                
                (#.Some _)
                (++ count)))
            0
            (list.indices (size array))))

(def: .public (vacancy array)
  (All [a] (-> (Array a) Nat))
  (n.- (..occupancy array) (..size array)))

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

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

(def: .public (example 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 (++ idx))
          
          (#.Some x)
          (if (p x)
            (#.Some x)
            (recur (++ idx))))
        #.None))))

(def: .public (example+ p xs)
  (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 (++ idx))
          
          (#.Some x)
          (if (p idx x)
            (#.Some [idx x])
            (recur (++ idx))))
        #.None))))

(def: .public (clone xs)
  (All [a] (-> (Array a) (Array a)))
  (let [arr_size (size xs)]
    (list\mix (function (_ idx ys)
                (case (read! idx xs)
                  #.None
                  ys

                  (#.Some x)
                  (write! idx x ys)))
              (empty arr_size)
              (list.indices arr_size))))

(def: .public (of_list xs)
  (All [a] (-> (List a) (Array a)))
  (product.right (list\mix (function (_ x [idx arr])
                             [(++ idx) (write! idx x arr)])
                           [0 (empty (list.size xs))]
                           xs)))

(def: underflow
  Nat
  (-- 0))

(def: (list|-default array)
  (All [a] (-> (Array a) (List a)))
  (loop [idx (-- (size array))
         output #.End]
    (case idx
      (^ (static ..underflow))
      output

      _
      (recur (-- idx)
             (case (read! idx array)
               (#.Some head)
               (#.Item head output)

               #.None
               output)))))

(def: (list|+default default array)
  (All [a] (-> a (Array a) (List a)))
  (loop [idx (-- (size array))
         output #.End]
    (case idx
      (^ (static ..underflow))
      output

      _
      (recur (-- idx)
             (#.Item (maybe.else default (read! idx array))
                     output)))))

(def: .public (list default array)
  (All [a] (-> (Maybe a) (Array a) (List a)))
  (case default
    (#.Some default)
    (list|+default default array)
    
    #.None
    (list|-default array)))

(implementation: .public (equivalence (^open ",\."))
  (All [a] (-> (Equivalence a) (Equivalence (Array a))))
  
  (def: (= xs ys)
    (let [sxs (size xs)
          sxy (size ys)]
      (and (n.= sxy sxs)
           (list\mix (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))))))

(implementation: .public monoid
  (All [a] (Monoid (Array a)))
  
  (def: identity (empty 0))

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

(implementation: .public functor
  (Functor Array)
  
  (def: (each f ma)
    (let [arr_size (size ma)]
      (if (n.= 0 arr_size)
        (empty arr_size)
        (list\mix (function (_ idx mb)
                    (case (read! idx ma)
                      #.None
                      mb

                      (#.Some x)
                      (write! idx (f x) mb)))
                  (empty arr_size)
                  (list.indices arr_size))
        ))))

(implementation: .public mix
  (Mix Array)
  
  (def: (mix 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 (++ idx))

            (#.Some value)
            (recur (f value so_far) (++ idx)))
          so_far)))))

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

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