about summary refs log tree commit diff stats
path: root/util.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util.lua')
-rw-r--r--util.lua41
1 files changed, 41 insertions, 0 deletions
diff --git a/util.lua b/util.lua new file mode 100644 index 0000000..98536a1 --- /dev/null +++ b/util.lua
@@ -0,0 +1,41 @@
1--- lam.util
2
3local util = {}
4
5function util.table (x)
6 if type(x) == "table" then
7 return x
8 else
9 return { x }
10 end
11end
12
13function util.pop (tbl)
14 return table.remove(tbl, 1)
15end
16
17function util.car (tbl)
18 return tbl[1]
19end
20
21function util.cdr (tbl)
22 local t = {}
23 for i = 2, #tbl do t[i-1] = tbl[i] end
24 return t
25end
26
27function util.reduce (tbl, seed, fn)
28 if #tbl == 0 then return seed end
29 return util.reduce(tbl, fn(seed, util.pop(tbl)), fn)
30end
31
32function util.map (fn, tbl)
33 local out = {}
34 for k, v in pairs(tbl) do
35 out[k] = fn(v)
36 end
37 return out
38end
39
40---
41return util