Run luacheck on CI to catch subtle errors.
Add a luacheckrc that ensures current code issues no warnings. We should
eventually try to remove many of these ignores.
As a new user, there were things I was skeptical about and after digging
in, these were my conclusions.
Compared to the simple and obvious lua wiki solutions, batteries' string
functions are slightly faster. GC is the same.
Test
local str = "hello world"
local fn = function()
local x = 0
if stringx.ends_with(str, "h") then
x = x + 1
end
if stringx.ends_with(str, "helll") then
x = x + 1
end
if stringx.ends_with(str, "helicopter") then
x = x + 1
end
end
local pretty = require "inspect"
print("stringx =", pretty({
time_taken = {measure.time_taken(fn, 10000)},
memory_taken = {measure.memory_taken(fn, 10000)}
}))
local function starts_with(str, prefix)
return str:find(prefix, 1, true) == 1
end
local function ends_with(str, ending)
return ending == "" or str:sub(-#ending) == ending
end
local fn = function()
local x = 0
if ends_with(str, "h") then
x = x + 1
end
if ends_with(str, "helll") then
x = x + 1
end
if ends_with(str, "helicopter") then
x = x + 1
end
end
print("find =", pretty({
time_taken = {measure.time_taken(fn, 10000)},
memory_taken = {measure.memory_taken(fn, 10000)}
}))
starts_with
===========
stringx = {
memory_taken = { 0, 0, 0 },
time_taken = { 1.5098012518138e-007, 9.988434612751e-008, 2.1699932403862e-005 }
}
find = {
memory_taken = { 0, 0, 0 },
time_taken = { 2.7349997544661e-007, 1.9988510757685e-007, 9.1999536380172e-006 }
}
ends_with
=========
stringx = {
memory_taken = { 0, 0, 0 },
time_taken = { 9.0479978825897e-008, 0, 2.5199959054589e-005 }
}
find = {
memory_taken = { 0, 0, 0 },
time_taken = { 2.1833006758243e-007, 1.9988510757685e-007, 6.1000464484096e-006 }
}
Replace copy with shallow_copy, deep_copy.
Replace overlay with shallow_overlay, deep_overlay.
Significant rewrite of these functions and their semantics to update to
the note above them.
Add corresponding tests.
The second loop doesn't care if the values are equal. It only checks if
the keys are there. The first loop already checked for equality.
Add tests to prove correctness using testy.lua.
Instead of only capturing 8 temps, pass resume to a function so we can
capture all returned values as ...
I might be missing a reason why you wouldn't do it like this.
Test
-- coro is an async() updated in love.update
coro:call(
function()
coro.wait(1)
print("jump")
self.gravity.y = -900
coro.wait(1)
print("fall")
self.gravity.y = 900
coroutine.yield "hello"
return "done"
end,
nil,
function(...)
print("complete", ...)
end)
- Now accepts a table from which it will pick the random value from,
instead of an index
- Changed function name to better reflect the way it now works