2024-02-13 06:05:36 +00:00
|
|
|
local list_utils = {}
|
|
|
|
|
|
|
|
function list_utils.find(list, predicate)
|
|
|
|
for _, value in ipairs(list) do
|
|
|
|
if predicate(value) then
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function list_utils.some(list, predicate)
|
|
|
|
return list_utils.find(list, predicate) ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function list_utils.includes(list, value)
|
|
|
|
return list_utils.find(list, function(item)
|
|
|
|
return item == value
|
|
|
|
end) ~= nil
|
|
|
|
end
|
|
|
|
|
2024-02-19 14:27:17 +00:00
|
|
|
function list_utils.map(list, fn)
|
|
|
|
local result = {}
|
|
|
|
|
|
|
|
for i, value in ipairs(list) do
|
|
|
|
table.insert(result, fn(value, i))
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2024-02-13 06:05:36 +00:00
|
|
|
return list_utils
|