Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reduce and reduceRight to work on falsy values #15

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Array/reduce.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local function reduce<T, U>(
local result = initReduction
local start = 1

if not result then
if result == nil then
result = array[1]
start = 2
end
Expand Down
10 changes: 10 additions & 0 deletions src/Array/reduce.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ return function()

expect(reduced).to.equal(-4)
end)

it("should reduce the array, even if the array has a falsy initial value", function()
local array = { true, false, false }

local reduced = Reduce(array, function(accumulator, value)
return accumulator or value
end, false)

expect(reduced).to.equal(true)
end)
end
2 changes: 1 addition & 1 deletion src/Array/reduceRight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ local function reduceRight<T, U>(
local result = initReduction
local start = #array

if not result then
if result == nil then
result = array[start]
start -= 1
end
Expand Down
10 changes: 10 additions & 0 deletions src/Array/reduceRight.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ return function()

expect(reduced).to.equal(0)
end)

it("should reduce the array from the right, even if the array has a falsy initial value", function()
local array = { true, false, true }

local reduced = ReduceRight(array, function(accumulator, value)
return accumulator or value
end, false)

expect(reduced).to.equal(true)
end)
end
Loading