diff options
author | Case Duckworth | 2024-02-21 09:28:49 -0600 |
---|---|---|
committer | Case Duckworth | 2024-02-21 09:28:49 -0600 |
commit | 70ec5254814f9531e5ca2024465d0e01130306b7 (patch) | |
tree | 45a4d49115eca436a8467ce4cebfc8cee1f6f9c6 /util.lua | |
download | lam-70ec5254814f9531e5ca2024465d0e01130306b7.tar.gz lam-70ec5254814f9531e5ca2024465d0e01130306b7.zip |
Initial commit
Diffstat (limited to 'util.lua')
-rw-r--r-- | util.lua | 41 |
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 | |||
3 | local util = {} | ||
4 | |||
5 | function util.table (x) | ||
6 | if type(x) == "table" then | ||
7 | return x | ||
8 | else | ||
9 | return { x } | ||
10 | end | ||
11 | end | ||
12 | |||
13 | function util.pop (tbl) | ||
14 | return table.remove(tbl, 1) | ||
15 | end | ||
16 | |||
17 | function util.car (tbl) | ||
18 | return tbl[1] | ||
19 | end | ||
20 | |||
21 | function util.cdr (tbl) | ||
22 | local t = {} | ||
23 | for i = 2, #tbl do t[i-1] = tbl[i] end | ||
24 | return t | ||
25 | end | ||
26 | |||
27 | function util.reduce (tbl, seed, fn) | ||
28 | if #tbl == 0 then return seed end | ||
29 | return util.reduce(tbl, fn(seed, util.pop(tbl)), fn) | ||
30 | end | ||
31 | |||
32 | function 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 | ||
38 | end | ||
39 | |||
40 | --- | ||
41 | return util | ||