aboutsummaryrefslogtreecommitdiff
path: root/lua/utils.lua
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-01-08 23:25:40 -0600
committerJoel Martin <github@martintribe.org>2015-01-08 23:25:40 -0600
commit9d42904e47c50c5ff2306da04993b2a32bc9cd16 (patch)
treee1b2d46a232e6573dc2c185967ebe988be3db973 /lua/utils.lua
parentfd888612ca589d7e1a46c36fc3fe12aed126f6a8 (diff)
downloadmal-9d42904e47c50c5ff2306da04993b2a32bc9cd16.tar.gz
mal-9d42904e47c50c5ff2306da04993b2a32bc9cd16.zip
Lua: all steps and self-hosting.
Also some misc docs/TODO updates.
Diffstat (limited to 'lua/utils.lua')
-rw-r--r--lua/utils.lua53
1 files changed, 53 insertions, 0 deletions
diff --git a/lua/utils.lua b/lua/utils.lua
new file mode 100644
index 0000000..1ed03e1
--- /dev/null
+++ b/lua/utils.lua
@@ -0,0 +1,53 @@
+local M = {}
+
+function M.try(f, catch_f)
+ local status, exception = pcall(f)
+ if not status then
+ catch_f(exception)
+ end
+end
+
+function M.instanceOf(subject, super)
+ super = tostring(super)
+ local mt = getmetatable(subject)
+
+ while true do
+ if mt == nil then return false end
+ if tostring(mt) == super then return true end
+ mt = getmetatable(mt)
+ end
+end
+
+--[[
+function M.isArray(o)
+ local i = 0
+ for _ in pairs(o) do
+ i = i + 1
+ if o[i] == nil then return false end
+ end
+ return true
+end
+]]--
+
+function M.map(func, obj)
+ local new_obj = {}
+ for i,v in ipairs(obj) do
+ new_obj[i] = func(v)
+ end
+ return new_obj
+end
+
+function M.dump(o)
+ if type(o) == 'table' then
+ local s = '{ '
+ for k,v in pairs(o) do
+ if type(k) ~= 'number' then k = '"'..k..'"' end
+ s = s .. '['..k..'] = ' .. M.dump(v) .. ','
+ end
+ return s .. '} '
+ else
+ return tostring(o)
+ end
+end
+
+return M