Skip to content

Instantly share code, notes, and snippets.

@ranisalt
Created October 23, 2014 02:45
Show Gist options
  • Save ranisalt/21d21ae9d87db51fe022 to your computer and use it in GitHub Desktop.
Save ranisalt/21d21ae9d87db51fe022 to your computer and use it in GitHub Desktop.
Lua stuff
function cleanPreviousIndentation(line)
return line:gsub("^%s+", ""):gsub("%s+$", "")
end
function escapeString(str)
return str:gsub("[()]", "%%%1")
end
function string:split(delimiter)
local array = {}
local first, last = string.find(self, '[^'..delimiter..']*'..delimiter)
while first do
array[#array + 1] = string.sub(self, first, last - 1)
self = string.sub(self, last + 1)
first, last = string.find(self, '[^'..delimiter..']*'..delimiter)
end
array[#array + 1] = self
return array
end
function indent(script)
local indented = script:split('\n')
local level = 0
for index, line in ipairs(script) do
if line:find("^end") or line:find("^}") then
level = level - 1
end
indented[index] = string.rep("\t", level)..line
if line:find("^if") or line:find("^while") or line:find("^= {") or line:find("^(local )?function)") then
level = level + 1
end
end
print(table.concat(indented, "\n"))
end
function indent_file(filename)
io.open(filename)
local code = io.read("*all")
io.close()
io.open("fixed_"..filename)
io.write(indent(escapeString(cleanPreviousIndentation(code))))
io.close()
end
-- função que retorna "true" se o elemento A for logicamente menor que B
local comp = function(a, b)
return a <= b
end
function quicksort(arr, left, right)
function partition(arr, left, right, index)
local pivot, _index = arr[index], left
arr[index], arr[right] = arr[right], arr[index]
for i = left, right - 1 do
if comp(arr[i], pivot) then
arr[i], arr[_index] = arr[_index], arr[i]
_index = _index + 1
end
end
arr[_index], arr[right] = arr[right], arr[_index]
return _index
end
if right > left then
local new_index = partition(arr, left, right, (left + right) / 2)
quicksort(arr, left, new_index - 1)
quicksort(arr, new_index + 1, right)
end
end
function quicksort(arr)
quicksort(arr, 1, #arr)
end
array = { 1, 5, 2, 17, 11, 3, 1, 22, 2, 37 }
quicksort(array)
for _, v in pairs(array) do
print(v)
end
local mt, index, band, bor, bxor, lshift, rshift = {}, 0, bit.band, bit.bor, bit.bxor, bit.lshift, bit.rshift
local init_rand = function(s)
if s == nil then
s = os.time()
end
mt[0] = band(s, 0xffffffff)
for i = 1, 623 do
mt[i] = band(0x6c078965 * bxor(mt[i - 1], rshift(mt[i - 1], 30)) + i, 0xffffffff)
end
end
local rand = function()
local matrix, y = {0, 0x9908b0df}
if index == 0 then
init_rand()
for i = 0, 623 do
y = bor(band(mt[i], 0x80000000), band(mt[(i + 1) % 624], 0x7ffffff))
mt[i] = bxor(mt[(i + 397) % 624], rshift(y, 1), matrix[band(y, 1) + 1])
end
end
index = (index + 1) % 624
y = mt[index]
y = bxor(y, rshift(y, 11))
y = band(bxor(y, lshift(y, 7), 0x9d2c5680))
y = band(bxor(y, lshift(y, 15), 0xefc60000))
y = bxor(y, rshift(y, 18))
return y % 0xffffffff
end
for _ = 1, 10000000 do
rand()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment