Lua API: simplify functional.reduce (#2788)
simplify code and remove `indentity or 0` VS `indentity` inconsistency.
(cherry-picked from commit 2b8782923a
)
This commit is contained in:
parent
0d1df0f2d7
commit
fa71ab4be0
1 changed files with 4 additions and 6 deletions
|
@ -87,13 +87,11 @@ function functional.map(input, formula)
|
|||
end
|
||||
|
||||
function functional.reduce(input, operator, identity)
|
||||
if #input == 0 then return identity end
|
||||
local value = operator(identity or 0, input[1])
|
||||
if #input == 1 then return value end
|
||||
for i = 2, #input do
|
||||
value = operator(value, input[i])
|
||||
local result = identity or 0
|
||||
for _, v in ipairs(input) do
|
||||
result = operator(result, v)
|
||||
end
|
||||
return value
|
||||
return result
|
||||
end
|
||||
|
||||
function functional.take_while(input, condition)
|
||||
|
|
Loading…
Add table
Reference in a new issue