1
0
mirror of https://github.com/kepler155c/opus synced 2025-01-27 15:44:46 +00:00

javascript style table functions

This commit is contained in:
kepler155c@gmail.com 2018-12-23 19:49:28 -05:00
parent 1264b0149b
commit 6343589183

View File

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