Skip to content

Commit

Permalink
Fixed #4318: Array.each_product fails on empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Apr 20, 2017
1 parent 7b43477 commit 0f65ea4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions spec/std/array_spec.cr
Expand Up @@ -1785,6 +1785,14 @@ describe "Array" do
end

describe "Array.each_product" do
it "one empty array" do
empty = [] of Int32
res = [] of Array(Int32)
Array.each_product([empty, [1, 2, 3]]) { |r| res << r }
Array.each_product([[1, 2, 3], empty]) { |r| res << r }
res.size.should eq(0)
end

it "single array" do
res = [] of Array(Int32)
Array.each_product([[1]]) { |r| res << r }
Expand Down
3 changes: 2 additions & 1 deletion src/array.cr
Expand Up @@ -1178,10 +1178,11 @@ class Array(T)
end

def self.each_product(arrays : Array(Array), reuse = false)
pool = arrays.map &.first
lens = arrays.map &.size
return if lens.any? &.==(0)

pool = arrays.map &.first

n = arrays.size
indices = Array.new(n, 0)

Expand Down

0 comments on commit 0f65ea4

Please sign in to comment.