2020-11-02 16:07:50 +00:00
|
|
|
-- Helper functions for mod_mix
|
2021-02-21 17:38:21 +00:00
|
|
|
local function find(array, f)
|
2020-11-02 16:07:50 +00:00
|
|
|
-- Searches for an element for which f returns true. The first element
|
|
|
|
-- and its index are returned. If none are found, then -1, nil is returned.
|
|
|
|
--
|
|
|
|
-- f is a function that takes the value and returns either true or false.
|
|
|
|
for i, v in pairs(array) do
|
|
|
|
if f(v) then return i, v; end
|
|
|
|
end
|
|
|
|
|
|
|
|
return -1, nil;
|
|
|
|
end
|
|
|
|
|
2021-02-21 17:38:21 +00:00
|
|
|
local function find_str(array, str)
|
2020-11-02 16:07:50 +00:00
|
|
|
-- Returns the index of str in array. -1 if array does not contain str
|
2021-02-28 10:29:21 +00:00
|
|
|
local i, _ = find(array, function(v) return v == str; end);
|
2020-11-02 16:07:50 +00:00
|
|
|
return i;
|
|
|
|
end
|
|
|
|
|
2021-02-28 10:29:21 +00:00
|
|
|
local function in_array(array, element)
|
|
|
|
-- Returns true if element is in array. False otherwise.
|
|
|
|
local i, _ = find(array, function(v) return v == element; end);
|
|
|
|
return i ~= -1;
|
|
|
|
end
|
|
|
|
|
2020-11-02 16:07:50 +00:00
|
|
|
return {
|
|
|
|
find_str = find_str,
|
|
|
|
find = find,
|
2021-02-28 10:29:21 +00:00
|
|
|
in_array = in_array,
|
2020-11-02 16:07:50 +00:00
|
|
|
};
|