From 70ec5254814f9531e5ca2024465d0e01130306b7 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 21 Feb 2024 09:28:49 -0600 Subject: Initial commit --- util.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 util.lua (limited to 'util.lua') diff --git a/util.lua b/util.lua new file mode 100644 index 0000000..98536a1 --- /dev/null +++ b/util.lua @@ -0,0 +1,41 @@ +--- lam.util + +local util = {} + +function util.table (x) + if type(x) == "table" then + return x + else + return { x } + end +end + +function util.pop (tbl) + return table.remove(tbl, 1) +end + +function util.car (tbl) + return tbl[1] +end + +function util.cdr (tbl) + local t = {} + for i = 2, #tbl do t[i-1] = tbl[i] end + return t +end + +function util.reduce (tbl, seed, fn) + if #tbl == 0 then return seed end + return util.reduce(tbl, fn(seed, util.pop(tbl)), fn) +end + +function util.map (fn, tbl) + local out = {} + for k, v in pairs(tbl) do + out[k] = fn(v) + end + return out +end + +--- +return util -- cgit 1.4.1-21-gabe81