From 8db3bb8cd021fc2e03926e16ad8bfc8c99619dfe Mon Sep 17 00:00:00 2001 From: Leah Hanson Date: Fri, 27 Sep 2013 08:39:02 -0400 Subject: [PATCH 1/2] make Permutation's next function not modify its second argument (s), to fix compability with filter, see issue #4381 --- base/combinatorics.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/base/combinatorics.jl b/base/combinatorics.jl index a5286f6fa24d5..b72b9a9785e56 100644 --- a/base/combinatorics.jl +++ b/base/combinatorics.jl @@ -236,6 +236,7 @@ function next(p::Permutations, s) # special case to generate 1 result for len==0 return (p.a,[1]) end + s = copy(s) perm = p.a[s] k = length(s)-1 while k > 0 && s[k] > s[k+1]; k -= 1; end From 1fa3b39214526b7b57534486eedefe93fdffacb5 Mon Sep 17 00:00:00 2001 From: Leah Hanson Date: Fri, 27 Sep 2013 13:09:31 -0400 Subject: [PATCH 2/2] added tests to check fix of #4381 --- test/combinatorics.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/combinatorics.jl b/test/combinatorics.jl index cc90140a72f2e..bf338bb9bea9f 100644 --- a/test/combinatorics.jl +++ b/test/combinatorics.jl @@ -6,6 +6,8 @@ a = randcycle(10) @test ipermute!(permute!([1:10], a),a) == [1:10] @test collect(combinations("abc",2)) == ["ab","ac","bc"] @test collect(permutations("abc")) == ["abc","acb","bac","bca","cab","cba"] +@test collect(filter(x->(iseven(x[1])),permutations([1,2,3]))) == {[2,1,3],[2,3,1]} +@test collect(filter(x->(iseven(x[3])),permutations([1,2,3]))) == {[1,3,2],[3,1,2]} @test collect(partitions(4)) == {[4], [3,1], [2,2], [2,1,1], [1,1,1,1]} @test collect(partitions(8,3)) == {[6,1,1], [5,2,1], [4,3,1], [4,2,2], [3,3,2]} @test collect(partitions([1,2,3])) == {{[1,2,3]}, {[1,2],[3]}, {[1,3],[2]}, {[1],[2,3]}, {[1],[2],[3]}}