aboutsummaryrefslogtreecommitdiff
path: root/utils.lua
blob: 2f6f25a4df866931d6b38326a0900f9a9ad39d8a (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
local utils
utils = {
  is_list = function(t)
    if type(t) ~= 'table' then
      return false
    end
    local i = 1
    for _ in pairs(t) do
      if t[i] == nil then
        return false
      end
      i = i + 1
    end
    return true
  end,
  size = function(t)
    do
      local n = 0
      for _ in pairs(t) do
        n = n + 1
      end
      return n
    end
  end,
  repr = function(x)
    local _exp_0 = type(x)
    if 'table' == _exp_0 then
      local mt = getmetatable(x)
      if mt and mt.__tostring then
        return mt.__tostring(x)
      elseif utils.is_list(x) then
        return "{" .. tostring(table.concat((function()
          local _accum_0 = { }
          local _len_0 = 1
          for _index_0 = 1, #x do
            local i = x[_index_0]
            _accum_0[_len_0] = utils.repr(i)
            _len_0 = _len_0 + 1
          end
          return _accum_0
        end)(), ", ")) .. "}"
      else
        return "{" .. tostring(table.concat((function()
          local _accum_0 = { }
          local _len_0 = 1
          for k, v in pairs(x) do
            _accum_0[_len_0] = "[" .. tostring(utils.repr(k)) .. "]= " .. tostring(utils.repr(v))
            _len_0 = _len_0 + 1
          end
          return _accum_0
        end)(), ", ")) .. "}"
      end
    elseif 'string' == _exp_0 then
      if x == "\n" then
        return "'\\n'"
      elseif not x:find([["]]) and not x:find("\n") and not x:find("\\") then
        return "\"" .. x .. "\""
      elseif not x:find([[']]) and not x:find("\n") and not x:find("\\") then
        return "\'" .. x .. "\'"
      else
        for i = 0, math.huge do
          local eq = ("="):rep(i)
          if not x:find("%]" .. tostring(eq) .. "%]") and not x:match(".*]" .. tostring(eq) .. "$") then
            if x:sub(1, 1) == "\n" then
              return "[" .. tostring(eq) .. "[\n" .. x .. "]" .. tostring(eq) .. "]"
            else
              return "[" .. tostring(eq) .. "[" .. x .. "]" .. tostring(eq) .. "]"
            end
          end
        end
      end
    else
      return tostring(x)
    end
  end,
  repr_if_not_string = function(x)
    if type(x) == 'string' then
      return x
    else
      return utils.repr(x)
    end
  end,
  split = function(str, sep)
    if sep == nil then
      sep = "%s"
    end
    local _accum_0 = { }
    local _len_0 = 1
    for chunk in str:gmatch("[^" .. tostring(sep) .. "]+") do
      _accum_0[_len_0] = chunk
      _len_0 = _len_0 + 1
    end
    return _accum_0
  end,
  remove_from_list = function(list, item)
    for i, list_item in ipairs(list) do
      if list_item == item then
        table.remove(list, i)
        return 
      end
    end
  end,
  accumulate = function(glue, co)
    if co == nil then
      glue, co = "", glue
    end
    local bits = { }
    for bit in coroutine.wrap(co) do
      table.insert(bits, bit)
    end
    return table.concat(bits, glue)
  end,
  range = function(start, stop, step)
    if stop == nil then
      start, stop, step = 1, start, 1
    elseif step == nil then
      step = 1
    elseif step == 0 then
      error("Range step cannot be zero.")
    end
    return setmetatable({
      start = start,
      stop = stop,
      step = step
    }, {
      __ipairs = function(self)
        local iter
        iter = function(self, i)
          if i <= (self.stop - self.start) / self.step then
            return i + 1, self.start + i * self.step
          end
        end
        return iter, self, 0
      end,
      __index = function(self, i)
        if type(i) ~= "Number" then
          return nil
        end
        if i % 1 ~= 0 then
          return nil
        end
        if i <= 0 or i - 1 > (self.stop - self.start) / self.step then
          return nil
        end
        return self.start + (i - 1) * self.step
      end,
      __len = function(self)
        local len = (self.stop - self.start) / self.step
        if len < 0 then
          len = 0
        end
        return len
      end
    })
  end,
  nth_to_last = function(list, n)
    return list[#list - n + 1]
  end,
  keys = function(t)
    local _accum_0 = { }
    local _len_0 = 1
    for k in pairs(t) do
      _accum_0[_len_0] = k
      _len_0 = _len_0 + 1
    end
    return _accum_0
  end,
  values = function(t)
    local _accum_0 = { }
    local _len_0 = 1
    for _, v in pairs(t) do
      _accum_0[_len_0] = v
      _len_0 = _len_0 + 1
    end
    return _accum_0
  end,
  set = function(list)
    local _tbl_0 = { }
    for _index_0 = 1, #list do
      local i = list[_index_0]
      _tbl_0[i] = true
    end
    return _tbl_0
  end,
  sum = function(t)
    do
      local tot = 0
      for _, x in pairs(t) do
        tot = tot + x
      end
      return tot
    end
  end,
  product = function(t)
    do
      local prod = 1
      for _, x in pairs(t) do
        prod = prod * x
      end
      return prod
    end
  end,
  all = function(t)
    for _, x in pairs(t) do
      if not x then
        return false
      end
    end
    return true
  end,
  any = function(t)
    for _, x in pairs(t) do
      if x then
        return true
      end
    end
    return false
  end,
  min = function(list, keyFn)
    if keyFn == nil then
      keyFn = (function(x)
        return x
      end)
    end
    assert(utils.is_list(list), "min() expects to be operating on a list")
    do
      local best = list[1]
      if type(keyFn) == 'table' then
        local keyTable = keyFn
        keyFn = function(k)
          return keyTable[k]
        end
      end
      for i = 2, #list do
        if keyFn(list[i]) < keyFn(best) then
          best = list[i]
        end
      end
      return best
    end
  end,
  max = function(list, keyFn)
    if keyFn == nil then
      keyFn = (function(x)
        return x
      end)
    end
    assert(utils.is_list(list), "min() expects to be operating on a list")
    do
      local best = list[1]
      if type(keyFn) == 'table' then
        local keyTable = keyFn
        keyFn = function(k)
          return keyTable[k]
        end
      end
      for i = 2, #list do
        if keyFn(list[i]) > keyFn(best) then
          best = list[i]
        end
      end
      return best
    end
  end,
  sort = function(list, keyFn, reverse)
    if keyFn == nil then
      keyFn = (function(x)
        return x
      end)
    end
    if reverse == nil then
      reverse = false
    end
    assert(utils.is_list(list), "min() expects to be operating on a list")
    if type(keyFn) == 'table' then
      local keyTable = keyFn
      keyFn = function(k)
        return keyTable[k]
      end
    end
    local comparison
    if reverse then
      comparison = (function(x, y)
        return (keyFn(x) > keyFn(y))
      end)
    else
      comparison = (function(x, y)
        return (keyFn(x) < keyFn(y))
      end)
    end
    return table.sort(list, comparison)
  end,
  equivalent = function(x, y)
    if x == y then
      return true
    end
    if type(x) ~= type(y) then
      return false
    end
    if type(x) ~= 'table' then
      return false
    end
    for k, v in pairs(x) do
      if y[k] ~= v then
        return false
      end
    end
    for k, v in pairs(y) do
      if x[k] ~= v then
        return false
      end
    end
    return true
  end,
  key_for = function(t, value)
    for k, v in pairs(t) do
      if v == value then
        return k
      end
    end
    return nil
  end,
  clamp = function(x, min, max)
    if x < min then
      return min
    elseif x > max then
      return max
    else
      return x
    end
  end,
  mix = function(min, max, amount)
    return (1 - amount) * min + amount * max
  end,
  sign = function(x)
    if x == 0 then
      return 0
    elseif x < 0 then
      return -1
    else
      return 1
    end
  end,
  round = function(x, increment)
    if increment == nil then
      increment = 1
    end
    if x >= 0 then
      return math.floor(x / increment + .5) * increment
    else
      return math.ceil(x / increment - .5) * increment
    end
  end
}
return utils