This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print "Hello, switch" | |
-- If the default case does not have to be handled, we can use the following auxiliary function: | |
local function switch(value) | |
-- Handing `cases` to the returned function allows the `switch()` function to be used with a syntax closer to c code (see the example below). | |
-- This is because lua allows the parentheses around a table type argument to be omitted if it is the only argument. | |
return function(cases) | |
-- The default case is achieved through the metatable mechanism of lua tables (the `__index` operation). | |
setmetatable(cases, cases) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local random = math.random | |
local function uuid() | |
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' | |
return string.gsub(template, '[xy]', function (c) | |
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb) | |
return string.format('%x', v) | |
end) | |
end |