mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 06:04:35 +00:00
added map_call for mapping a sequence with a method call (useful for object-y collections)
This commit is contained in:
parent
7366473f32
commit
b68081be53
@ -99,6 +99,23 @@ function functional.map_field(t, k)
|
||||
return result
|
||||
end
|
||||
|
||||
--maps a sequence
|
||||
-- if f is a string method name like "position", {a, b} -> {a:f(...), b:f(...)}
|
||||
-- if f is function reference like player.get_position, {a, b} -> {f(a, ...), f(b, ...)}
|
||||
-- (automatically drops any nils to keep a sequence)
|
||||
function functional.map_call(t, f, ...)
|
||||
local result = {}
|
||||
for i = 1, #t do
|
||||
local v = t[i]
|
||||
local f = type(f) == "function" and f or v[f]
|
||||
v = f(v, ...)
|
||||
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
|
||||
|
@ -68,6 +68,7 @@ end
|
||||
for _, v in ipairs({
|
||||
"map",
|
||||
"map_field",
|
||||
"map_call",
|
||||
"filter",
|
||||
"remove_if",
|
||||
"zip",
|
||||
|
Loading…
Reference in New Issue
Block a user