mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-12-04 21:13:50 +01:00
weac.table*: fix documentation, table.find
This commit is contained in:
parent
da97890e0c
commit
163f4fc0fd
@ -5,10 +5,10 @@
|
|||||||
-- Warning: This function mutates `target`!
|
-- Warning: This function mutates `target`!
|
||||||
-- @param source table The source to take values from
|
-- @param source table The source to take values from
|
||||||
-- @param target table The target to write values to
|
-- @param target table The target to write values to
|
||||||
local function table_apply(source, target)
|
local function apply(source, target)
|
||||||
for key, value in pairs(source) do
|
for key, value in pairs(source) do
|
||||||
target[key] = value
|
target[key] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return table_apply
|
return apply
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
-- @param tbl table The table to look in.
|
-- @param tbl table The table to look in.
|
||||||
-- @param target any The target to look for.
|
-- @param target any The target to look for.
|
||||||
-- @returns bool Whether the table contains the given target or not.
|
-- @returns bool Whether the table contains the given target or not.
|
||||||
local function table_contains(tbl, target)
|
local function contains(tbl, target)
|
||||||
for key, value in ipairs(tbl) do
|
for key, value in ipairs(tbl) do
|
||||||
if value == target then return true end
|
if value == target then return true end
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
return table_contains
|
return contains
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
-- @param tbl table The table of values to filter.
|
-- @param tbl table The table of values to filter.
|
||||||
-- @param func function<any, number>:bool The filter function to execute - should return a boolean value indicating whether the item provided as the first argument should be kept
|
-- @param func function<any, number>:bool The filter function to execute - should return a boolean value indicating whether the item provided as the first argument should be kept
|
||||||
-- @returns table A new table containing the values that the given function returned true for.
|
-- @returns table A new table containing the values that the given function returned true for.
|
||||||
local function table_filter(tbl, func)
|
local function filter(tbl, func)
|
||||||
local result = {}
|
local result = {}
|
||||||
for i,value in ipairs(tbl) do
|
for i,value in ipairs(tbl) do
|
||||||
if func(value, i) then
|
if func(value, i) then
|
||||||
@ -20,4 +20,4 @@ local function table_filter(tbl, func)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
return table_filter
|
return filter
|
||||||
|
@ -12,10 +12,7 @@
|
|||||||
-- 2. `i` (number): The index in the table that the value can be found at
|
-- 2. `i` (number): The index in the table that the value can be found at
|
||||||
-- 3. `tbl` (table): The original table.
|
-- 3. `tbl` (table): The original table.
|
||||||
-- @return any|nil The first element in the table that satisfies the predicate, or nil if no such element is found.
|
-- @return any|nil The first element in the table that satisfies the predicate, or nil if no such element is found.
|
||||||
function table_find(tbl, func)
|
function find(tbl, func)
|
||||||
end
|
|
||||||
|
|
||||||
function table_find(tbl, func)
|
|
||||||
for i,value in ipairs(tbl) do
|
for i,value in ipairs(tbl) do
|
||||||
if func(value, i, tbl) then
|
if func(value, i, tbl) then
|
||||||
return value
|
return value
|
||||||
@ -23,4 +20,4 @@ function table_find(tbl, func)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return table_find
|
return find
|
@ -9,11 +9,11 @@ local table_unpack = dofile(wea_c.modpath.."/utils/table/table_unpack.lua")
|
|||||||
-- @param tbl table The table to fetch items from.
|
-- @param tbl table The table to fetch items from.
|
||||||
-- @param count number The number of items to fetch from the end of the table.
|
-- @param count number The number of items to fetch from the end of the table.
|
||||||
-- @returns table A table containing the last count items from the given table.
|
-- @returns table A table containing the last count items from the given table.
|
||||||
local function table_get_last(tbl, count)
|
local function get_last(tbl, count)
|
||||||
return {table_unpack(
|
return {table_unpack(
|
||||||
tbl,
|
tbl,
|
||||||
math.max(0, (#tbl) - (count - 1))
|
math.max(0, (#tbl) - (count - 1))
|
||||||
)}
|
)}
|
||||||
end
|
end
|
||||||
|
|
||||||
return table_get_last
|
return get_last
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
-- @param tbl table The table to operate on.
|
-- @param tbl table The table to operate on.
|
||||||
-- @param func function<any>:any|nil The function to execute on every item in the table.
|
-- @param func function<any>:any|nil The function to execute on every item in the table.
|
||||||
-- @returns table A new table containing the return values of the function.
|
-- @returns table A new table containing the return values of the function.
|
||||||
local function table_map(tbl, func)
|
local function map(tbl, func)
|
||||||
local result = {}
|
local result = {}
|
||||||
for i,value in ipairs(tbl) do
|
for i,value in ipairs(tbl) do
|
||||||
local newval = func(value, i)
|
local newval = func(value, i)
|
||||||
@ -15,4 +15,4 @@ local function table_map(tbl, func)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
return table_map
|
return map
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
-- @param tbl The table to iterate over.
|
-- @param tbl The table to iterate over.
|
||||||
-- @param func The function to call for every element in tbl. Will be passed the following arguments: accumulator, value, index, table. Of course, the provided function need not take this many arguments.
|
-- @param func The function to call for every element in tbl. Will be passed the following arguments: accumulator, value, index, table. Of course, the provided function need not take this many arguments.
|
||||||
-- @param initial_value The initial value of the accumulator.
|
-- @param initial_value The initial value of the accumulator.
|
||||||
local function table_reduce(tbl, func, initial_value)
|
local function reduce(tbl, func, initial_value)
|
||||||
local acc = initial_value
|
local acc = initial_value
|
||||||
for key, value in pairs(tbl) do
|
for key, value in pairs(tbl) do
|
||||||
acc = func(acc, value, key, tbl)
|
acc = func(acc, value, key, tbl)
|
||||||
@ -13,4 +13,4 @@ local function table_reduce(tbl, func, initial_value)
|
|||||||
return acc
|
return acc
|
||||||
end
|
end
|
||||||
|
|
||||||
return table_reduce
|
return reduce
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
-- @param new_line string key value pair delimiter
|
-- @param new_line string key value pair delimiter
|
||||||
-- @param max_depth number max recursion depth (optional)
|
-- @param max_depth number max recursion depth (optional)
|
||||||
-- @return string concatenated table pairs
|
-- @return string concatenated table pairs
|
||||||
local function table_tostring(tbl, sep, new_line, max_depth)
|
local function tostring(tbl, sep, new_line, max_depth)
|
||||||
if type(sep) ~= "string" then sep = ": " end
|
if type(sep) ~= "string" then sep = ": " end
|
||||||
if type(new_line) ~= "string" then new_line = ", " end
|
if type(new_line) ~= "string" then new_line = ", " end
|
||||||
if type(max_depth) == "number" then max_depth = {depth=0,max=max_depth}
|
if type(max_depth) == "number" then max_depth = {depth=0,max=max_depth}
|
||||||
@ -28,4 +28,4 @@ end
|
|||||||
-- Test:
|
-- Test:
|
||||||
-- /lua v1 = { x= 0.335, facing= { axis= "z", sign= -1 } }; print(worldeditadditions.table.tostring(v1))
|
-- /lua v1 = { x= 0.335, facing= { axis= "z", sign= -1 } }; print(worldeditadditions.table.tostring(v1))
|
||||||
|
|
||||||
return table_tostring
|
return tostring
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
-- See also `worldeditadditions_core.Set`.
|
-- See also `worldeditadditions_core.Set`.
|
||||||
-- @param tbl table The table of values to make unique.
|
-- @param tbl table The table of values to make unique.
|
||||||
-- @returns table A new table containing the values of the given table appearing at most once.
|
-- @returns table A new table containing the values of the given table appearing at most once.
|
||||||
local function table_unique(tbl)
|
local function unique(tbl)
|
||||||
local newtbl = {}
|
local newtbl = {}
|
||||||
for i,value in ipairs(tbl) do
|
for i,value in ipairs(tbl) do
|
||||||
local seen = false
|
local seen = false
|
||||||
@ -23,4 +23,4 @@ local function table_unique(tbl)
|
|||||||
return newtbl
|
return newtbl
|
||||||
end
|
end
|
||||||
|
|
||||||
return table_unique
|
return unique
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
-- @param [offset=0] number The offset at which to start unpacking.
|
-- @param [offset=0] number The offset at which to start unpacking.
|
||||||
-- @param [count=nil] number The number of items to unpack. Defaults to unpack all remaining items after `offset`.
|
-- @param [count=nil] number The number of items to unpack. Defaults to unpack all remaining items after `offset`.
|
||||||
-- @returns any... The selected items unpacked from the table.
|
-- @returns any... The selected items unpacked from the table.
|
||||||
|
-- @example Lua version agnostic unpack
|
||||||
|
-- print(worldeditadditions_core.table.unpack({1 = "apple", 2 = "orange"}))
|
||||||
local function table_unpack(tbl, offset, count)
|
local function table_unpack(tbl, offset, count)
|
||||||
---@diagnostic disable-next-line: deprecated
|
---@diagnostic disable-next-line: deprecated
|
||||||
if type(unpack) == "function" then
|
if type(unpack) == "function" then
|
||||||
|
Loading…
Reference in New Issue
Block a user