Use 'pairs' instead of 'ipairs' so that functions don't break on lists with nil, fix various small bugs
This commit is contained in:
parent
153f30f381
commit
64fd49e33b
11 changed files with 62 additions and 25 deletions
|
@ -1,5 +1,5 @@
|
|||
function all(list)
|
||||
for _, value in ipairs(list) do
|
||||
for _, value in pairs(list) do
|
||||
if (value == nil) then
|
||||
return false
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
function containedIn(list, a)
|
||||
for _, value in ipairs(list) do
|
||||
for _, value in pairs(list) do
|
||||
if (value == a) then
|
||||
return true
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
function first_match_of(tags, result, order_of_keys, table)
|
||||
for _, key in ipairs(order_of_keys) do
|
||||
for _, key in pairs(order_of_keys) do
|
||||
local v = tags[key]
|
||||
if (v ~= nil) then
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ function head(ls)
|
|||
if(ls == nil) then
|
||||
return nil
|
||||
end
|
||||
for _, v in ipairs(ls) do
|
||||
for _, v in pairs(ls) do
|
||||
if(v ~= nil) then
|
||||
return v
|
||||
end
|
||||
|
|
|
@ -1,11 +1,22 @@
|
|||
function applyIfNeeded(f, arg)
|
||||
if(f == nil) then
|
||||
return nil
|
||||
end
|
||||
if(type(f) == "function") then
|
||||
return f(arg)
|
||||
else
|
||||
return f
|
||||
end
|
||||
end
|
||||
|
||||
function if_then_else_dotted(conditionf, thnf, elsef, arg)
|
||||
local condition = conditionf(arg);
|
||||
local condition = applyIfNeeded(conditionf, arg);
|
||||
if (condition) then
|
||||
return thnf(arg)
|
||||
return applyIfNeeded(thnf, arg)
|
||||
else
|
||||
if(elsef == nil) then
|
||||
return nil
|
||||
end
|
||||
return elsef(arg) -- if no third parameter is given, 'els' will be nil
|
||||
return applyIfNeeded(elsef, arg) -- if no third parameter is given, 'els' will be nil
|
||||
end
|
||||
end
|
|
@ -1,10 +1,12 @@
|
|||
function max(list)
|
||||
local max
|
||||
for _, value in ipairs(list) do
|
||||
if (max == nil) then
|
||||
max = value
|
||||
elseif (max < value) then
|
||||
max = value
|
||||
for _, value in pairs(list) do
|
||||
if (value == nil) then
|
||||
if (max == nil) then
|
||||
max = value
|
||||
elseif (max < value) then
|
||||
max = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
function min(list)
|
||||
local min
|
||||
for _, value in ipairs(list) do
|
||||
if (min == nil) then
|
||||
min = value
|
||||
elseif (min > value) then
|
||||
min = value
|
||||
for _, value in pairs(list) do
|
||||
if(value ~= nil) then
|
||||
if (min == nil) then
|
||||
min = value
|
||||
elseif (value < min) then
|
||||
min = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
function multiply(list)
|
||||
local factor = 1
|
||||
for _, value in ipairs(list) do
|
||||
factor = factor * value
|
||||
for _, value in pairs(list) do
|
||||
if (value ~= nil) then
|
||||
factor = factor * value
|
||||
end
|
||||
end
|
||||
return factor;
|
||||
end
|
|
@ -22,6 +22,15 @@ function parse(string)
|
|||
end
|
||||
end
|
||||
|
||||
if(string:match("%d*:%d*")) then
|
||||
-- duration in minute
|
||||
local duration = 0
|
||||
for part in string:gmatch "%d*" do
|
||||
duration = duration * 60 + tonumber(part)
|
||||
end
|
||||
return duration
|
||||
end
|
||||
|
||||
|
||||
return tonumber(string)
|
||||
end
|
|
@ -1,10 +1,12 @@
|
|||
function sum(list)
|
||||
local sum = 1
|
||||
for _, value in ipairs(list) do
|
||||
if(value == 'yes' or value == 'true') then
|
||||
value = 1
|
||||
local sum = 0
|
||||
for _, value in pairs(list) do
|
||||
if(value ~= nil) then
|
||||
if(value == 'yes' or value == 'true') then
|
||||
value = 1
|
||||
end
|
||||
sum = sum + value
|
||||
end
|
||||
sum = sum + value
|
||||
end
|
||||
return sum;
|
||||
end
|
|
@ -1,8 +1,17 @@
|
|||
failed_tests = false
|
||||
function unit_test(f, fname, index, expected, parameters, tags)
|
||||
if (f == nil) then
|
||||
print("Trying to unit test " .. fname .. " but this function is not defined")
|
||||
failed_tests = true
|
||||
return
|
||||
end
|
||||
local result = {attributes_to_keep = {}}
|
||||
local actual = f(parameters, tags, result)
|
||||
if (tostring(actual) ~= expected) then
|
||||
if(expected == "null" and actual == nil) then
|
||||
-- OK!
|
||||
elseif(tonumber(actual) and tonumber(expected) and tonumber(actual) == tonumber(expected)) then
|
||||
-- OK!
|
||||
elseif (tostring(actual) ~= expected) then
|
||||
print("[" .. fname .. "] " .. index .. " failed: expected " .. expected .. " but got " .. tostring(actual))
|
||||
failed_tests = true
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue