2021-12-05 12:10:59 +01:00
|
|
|
function split(text, sep)
|
|
|
|
local parts = {}
|
|
|
|
local start = 0
|
|
|
|
local from, to = text:find(sep)
|
|
|
|
while from ~= nil do
|
|
|
|
table.insert(parts, text:sub(start, from - 1))
|
|
|
|
text = text:sub(to + 1)
|
|
|
|
from, to = text:find(sep)
|
|
|
|
end
|
|
|
|
table.insert(parts, text)
|
|
|
|
return parts
|
|
|
|
end
|
|
|
|
|
|
|
|
function sign(n)
|
|
|
|
if n == 0 then return 0
|
|
|
|
elseif n < 0 then return -1
|
|
|
|
else return 1
|
|
|
|
end
|
|
|
|
end
|
2021-12-07 08:29:45 +01:00
|
|
|
|
|
|
|
function foreach(t, f)
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
t[k] = f(v)
|
|
|
|
end
|
|
|
|
end
|
2021-12-08 08:37:36 +01:00
|
|
|
|
2021-12-15 10:51:53 +01:00
|
|
|
function printtable(t, nonewline)
|
2021-12-08 08:37:36 +01:00
|
|
|
for k, v in pairs(t) do
|
2021-12-15 10:51:53 +01:00
|
|
|
io.write(k..":")
|
|
|
|
if type(v) == "table" then
|
|
|
|
io.write("(")
|
|
|
|
printtable(v, true)
|
|
|
|
io.write(") ")
|
|
|
|
else
|
2021-12-18 15:51:21 +01:00
|
|
|
io.write(tostring(v).." ")
|
2021-12-15 10:51:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if not nonewline then
|
|
|
|
io.write("\n")
|
2021-12-08 08:37:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function count(t, pred)
|
|
|
|
local count = 0
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if pred(v) then count = count + 1 end
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|
2021-12-08 13:59:12 +01:00
|
|
|
|
|
|
|
function foldl(t, f, a)
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
a = f(a, v)
|
|
|
|
end
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
|
|
|
|
function sum(t)
|
|
|
|
return foldl(t, function (x, y) return x + y end, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function char(s, index)
|
|
|
|
return string.char(string.byte(s, index))
|
|
|
|
end
|
|
|
|
|
|
|
|
function chars(s)
|
|
|
|
local index = 0
|
|
|
|
return function()
|
|
|
|
if index < #s then
|
|
|
|
index = index + 1
|
|
|
|
return char(s, index)
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-16 08:56:01 +01:00
|
|
|
|
|
|
|
function bin2dec(str)
|
|
|
|
local v = 0
|
|
|
|
for c in chars(str) do
|
|
|
|
v = v * 2
|
|
|
|
if c == "1" then v = v + 1 end
|
|
|
|
end
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
2021-12-17 10:03:15 +01:00
|
|
|
function euler(n)
|
|
|
|
return n * (n + 1) / 2
|
|
|
|
end
|