mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 14:14:36 +00:00
[added] add group-by function to group elements by the return of a provided function
This commit is contained in:
parent
6f0945a675
commit
d882127bda
@ -263,4 +263,17 @@ function functional.find_match(t, f)
|
||||
return nil
|
||||
end
|
||||
|
||||
-- returns a table where the elements in t are grouped by the result of f on each element.
|
||||
function functional.group_by(t, f)
|
||||
local result = {}
|
||||
for i, v in ipairs(t) do
|
||||
local group = f(v)
|
||||
if result[group] == nil then
|
||||
result[group] = {}
|
||||
end
|
||||
table.insert(result[group], v)
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
return functional
|
||||
|
Loading…
Reference in New Issue
Block a user