aboutsummaryrefslogtreecommitdiffstats
path: root/alv-lib/array-.moon
blob: aeb682051045f6c07853fa67b6c2cb95950631fe (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
import Array, Op, PureOp, Builtin, Constant, Error, const, sig, evt, T from require 'alv.base'
import Cell from require 'alv.cell'
import Tag from require 'alv.tag'
builtins = require 'alv.builtins'

unpack or= table.unpack
any = sig! / evt!

get = Constant.meta
  meta:
    name: 'get'
    summary: "Index into an array."
    examples: { '(get array i)' }
    description: "Get the value at index `i` (starting at 0).

`i` has to be a constant expression."

  value: class extends PureOp
    pattern: any + const.num
    type: (inputs) =>
      { array, i } = inputs
      array\type!\get i.result!

    tick: =>
      { array, i } = @unwrap_all!
      @out\set array[i + 1]

set = Constant.meta
  meta:
    name: 'set'
    summary: "Update a value in an array."
    examples: { '(set array i val)' }
    description: "Set the value for `i` to `val`.

`i` has to be a constant expression. This is a pure op, so at most one of
`array` and `val` may be a !-stream."

  value: class extends PureOp
    pattern: any + const.num + any
    type: (inputs) =>
      { array, i, val } = inputs
      type = array\type!
      expected = type\get i.result!

      if expected ~= val\type!
        msg = string.format "expected value of type %s, not %s",
                            expected, val\type!
        error Error 'argument', msg

      type

    tick: =>
      { array, key, val } = @unwrap_all!

      array = [v for v in *array]
      array[key + 1] = val

      @out\set array

head = Constant.meta
  meta:
    name: 'head'
    summary: "Get the first element from an array."
    examples: { '(head array)' }

  value: class extends PureOp
    pattern: any*1
    type: (inputs) =>
      type = inputs[1]\type!

      assert type.__class == Array, Error 'argument', "expected an Array"
      assert type.size > 0, Error 'argument', "cannot get head of empty Array"

      type.type

    tick: =>
      { array } = @unwrap_all!
      @out\set array[1]

tail = Constant.meta
  meta:
    name: 'tail'
    summary: "Get everything except the first element from an array."
    examples: { '(tail array)' }

  value: class extends PureOp
    pattern: any*1
    type: (inputs) =>
      type = inputs[1]\type!

      assert type.__class == Array, Error 'argument', "expected an Array"
      assert type.size > 0, Error 'argument', "cannot get tail of empty Array"

      Array type.size - 1, type.type

    tick: =>
      { array } = @unwrap_all!
      @out\set [v for v in *array[2,]]

prepend = Constant.meta
  meta:
    name: 'prepend'
    summary: "Prepend a new value at the start of an array."
    examples: { '(prepend array val)' }
    description: "Prepend `val` to `array` at index `0`, moving other values back.

This is a pure op, so at most one of `array` and `val` may be a !-stream."

  value: class extends PureOp
    pattern: any + any
    type: (inputs) =>
      { array, val } = inputs
      type = array\type!

      if val\type! ~= type.type
        msg = string.format "expected value of type %s, not %s",
                            type.type, val\type!
        error Error 'argument', msg

      Array type.size + 1, type.type

    tick: =>
      { array, val } = @unwrap_all!

      array = [v for v in *array]
      table.insert array, 1, val

      @out\set array

insert = Constant.meta
  meta:
    name: 'insert'
    summary: "Insert a new value into an array."
    examples: { '(insert array i val)' }
    description: "Insert `val` into `array` at `i`, moving other values back if
necessary.

`i` has to be a constant expression. This is a pure op, so at most one of
`array` and `val` may be a !-stream."

  value: class extends PureOp
    pattern: any + const.num + any
    type: (inputs) =>
      { array, i, val } = inputs
      type = array\type!
      i = i.result!

      if i > type.size or i < 0
        error Error 'argument', "index '#{i}' out of range!"
      if val\type! ~= type.type
        msg = string.format "expected value of type %s, not %s",
                            type.type, val\type!
        error Error 'argument', msg

      Array type.size + 1, type.type

    tick: =>
      { array, i, val } = @unwrap_all!

      array = [v for v in *array]
      table.insert array, i + 1, val

      @out\set array

remove = Constant.meta
  meta:
    name: 'remove'
    summary: "Remove a value from an Array."
    examples: { '(remove array i)' }
    description: "Removes the value at index `i` from `array`.

`i` has to be a constant expression."

  value: class extends PureOp
    pattern: any + const.num
    type: (inputs) =>
      { array, i } = inputs
      type = array\type!

      -- check index range
      type\get i.result!

      Array type.size - 1, type.type

    tick: =>
      { array, i, val } = @unwrap_all!

      array = [v for v in *array]
      table.remove array, i + 1

      @out\set array

size = Constant.meta
  meta:
    name: 'size'
    summary: "Get the size of an array."
    examples: { '(size array)' }

  value: class extends Op
    setup: (inputs) =>
      super {}

      assert #inputs == 1, Error 'argument', "expected exactly one argument"
      type = inputs[1]\type!
      assert type.__class == Array, Error 'argument', "expected an Array"

      @out = Constant.num type.size

concat = Constant.meta
  meta:
    name: 'concat'
    summary: "Concatenate Arrays."
    examples: { '(concat arr1 arr2 [arr3…])' }

  value: class extends PureOp
    pattern: any\rep 2
    type: (inputs) =>
      size = 0
      type = inputs[1]\type!.type

      for input in *inputs
        array = input\type!

        if array.type ~= type
          msg = string.format "Cannot concatenate different arrays %s, %s",
                inputs[1]\type!, array
          error Error 'argument', msg

        size += array.size

      Array size, type

    tick: =>
      arrays = @unwrap_all!
      out = {}

      for array in *arrays
        for val in *array
          table.insert out, val

      @out\set out

array_constr = builtins!\get('array').result
map = Constant.meta
  meta:
    name: 'map'
    summary: "Apply an function to each value in an array."
    examples: { '(map array fn)' }
    description: "
Invokes `fn` once for each element in `array` and returns an array of the results.
`fn` must take one argument and return the same type consistently."

  value: class extends Builtin
    eval: (scope, tail) =>
      L\trace "evaling #{@}"
      assert #tail == 2, "'map' takes exactly two arguments"
      tail = [L\push t\eval, scope for t in *tail]
      { array, fn } = tail

      fndef = fn.result
      assert fn\type! == T.fndef, "fn has to be a fndef"
      array_type = array\type!
      assert array_type.__class == Array, Error 'argument', "expected an Array"

      invocations = for i=1, array_type.size
        tag_o = @tag\clone Tag.parse tostring i
        tag_i = @tag\clone tag_o
        Cell tag_o, {
          with Constant.literal T.fndef, fndef!, 'fn'
            .meta = fndef.meta
          Cell tag_i, {
            Constant.literal T.opdef, get!, 'get'
            Constant.literal array_type, array.result!, 'array'
            Constant.num i-1
          }
        }

      tag = @tag\clone Tag.parse '-1'
      inner = Cell tag, {
        Constant.literal T.opdef, array_constr, 'array'
        unpack invocations
      }
      super inner\eval scope


Constant.meta
  meta:
    name: 'array'
    summary: "Utilities for dealing with arrays."

  value:
    :get, :set
    :head, :tail, :prepend
    :insert, :remove
    :map

    :size, :concat