javascript style table functions

This commit is contained in:
kepler155c@gmail.com 2018-12-23 19:49:28 -05:00
parent 1264b0149b
commit 6343589183
1 changed files with 21 additions and 3 deletions

View File

@ -119,13 +119,13 @@ function Util.signum(num)
end
end
function Util.clamp(lo, n, hi)
function Util.clamp(lo, num, hi)
if num <= lo then
return lo
elseif num >= hi then
return hi
else
return n
return num
end
end
@ -314,15 +314,33 @@ function Util.size(list)
return 0
end
local function isArray(value)
-- dubious
return type(value) == "table" and (value[1] or next(value) == nil)
end
function Util.removeByValue(t, e)
for k,v in pairs(t) do
if v == e then
table.remove(t, k)
if isArray(t) then
table.remove(t, k)
else
t[k] = nil
end
break
end
end
end
function Util.every(t, fn)
for _,v in pairs(t) do
if not fn(v) then
return false
end
end
return true
end
function Util.each(list, func)
for index, value in pairs(list) do
func(value, index, list)