wea.min, wea.max: add API functions

This commit is contained in:
Starbeamrainbowlabs 2021-05-29 23:13:10 +01:00
parent c7c6a848dc
commit b23f353b19
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -19,12 +19,43 @@ function worldeditadditions.sum(list)
return sum
end
--- Calculates the mean of all the numbers in the given list.
-- @param list number[] The list (table) of numbers to calculate the mean for.
-- @returns The mean of the numbers in the given table.
function worldeditadditions.average(list)
if #list == 0 then return 0 end
return worldeditadditions.sum(list) / #list
end
--- Finds the minimum value in the given list.
-- @param list number[] The list (table) of numbers to find the minimum value of.
-- @returns number The minimum value in the given list.
function worldeditadditions.min(list)
if #list == 0 then return nil end
local min = nil
for i,value in ipairs(list) do
if min == nil or min > value then
min = value
end
end
return min
end
--- Finds the maximum value in the given list.
-- @param list number[] The list (table) of numbers to find the maximum value of.
-- @returns number The maximum value in the given list.
function worldeditadditions.max(list)
if #list == 0 then return nil end
local max = nil
for i,value in ipairs(list) do
if max == nil or max < value then
max = value
end
end
return max
end
--- Returns the minetest.get_us_time() in ms
-- @return float
function worldeditadditions.get_ms_time()