Skip to content

Instantly share code, notes, and snippets.

@gvx
Created February 16, 2009 13:59
Show Gist options
  • Save gvx/65172 to your computer and use it in GitHub Desktop.
Save gvx/65172 to your computer and use it in GitHub Desktop.
default definitions for Lua
-- defdef.lua
-- default definitions for lua
-- By Robin Wellner (gvx)
-- I hereby waive copyright and related or neighboring rights to this work
-- See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
-- Evaluate truth like in Python
function is_true (arg)
if arg == '' or arg == 0 then
return false -- empty string or zero returns false
elseif type(arg) == 'table' then
for k, v in pairs(arg) do
return true -- non-empty table returns true
end
return false -- empty table returns false
else
return arg -- all else is evaluated by lua
end
end
-- Print a table
function printtbl(tbl)
for k,v in pairs(tbl) do
print(k .. ': ' .. v)
end
end
-- Print a sequence
function printseq(tbl)
for k,v in pairs(tbl) do
print(v)
end
end
-- Sequence "type"
function seq(tbl)
local mt = {tostring=function () '['..table.concat(tbl, ', ')..']' end}
setmetatable(tbl, mt)
return tbl
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment