added functional.map_field for quick extraction of some field of a sequence of objects

This commit is contained in:
Max Cahill 2022-06-07 15:38:16 +10:00
parent dbce7e6083
commit 7366473f32
2 changed files with 14 additions and 0 deletions

View File

@ -86,6 +86,19 @@ end
--alias --alias
functional.remap = functional.map_inplace functional.remap = functional.map_inplace
--maps a sequence {a, b, c} -> {a[k], b[k], c[k]}
-- (automatically drops any nils to keep a sequence)
function functional.map_field(t, k)
local result = {}
for i = 1, #t do
local v = t[i][k]
if v ~= nil then
table.insert(result, v)
end
end
return result
end
--maps a sequence into a new index space (see functional.map) --maps a sequence into a new index space (see functional.map)
-- the function may return an index where the value will be stored in the result -- the function may return an index where the value will be stored in the result
-- if no index (or a nil index) is provided, it will insert as normal -- if no index (or a nil index) is provided, it will insert as normal

View File

@ -67,6 +67,7 @@ end
--(common case where something returns another sequence for chaining) --(common case where something returns another sequence for chaining)
for _, v in ipairs({ for _, v in ipairs({
"map", "map",
"map_field",
"filter", "filter",
"remove_if", "remove_if",
"zip", "zip",