mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-25 23:24:35 +00:00
added functional.map_field for quick extraction of some field of a sequence of objects
This commit is contained in:
parent
dbce7e6083
commit
7366473f32
@ -86,6 +86,19 @@ end
|
||||
--alias
|
||||
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)
|
||||
-- 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
|
||||
|
@ -67,6 +67,7 @@ end
|
||||
--(common case where something returns another sequence for chaining)
|
||||
for _, v in ipairs({
|
||||
"map",
|
||||
"map_field",
|
||||
"filter",
|
||||
"remove_if",
|
||||
"zip",
|
||||
|
Loading…
Reference in New Issue
Block a user