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:
Vasya 2018-03-30 20:23:39 +03:00 committed by Celtic Minstrel
parent 0d1df0f2d7
commit fa71ab4be0

View file

@ -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)