lib/epitools/permutations.rb in epitools-0.5.103 vs lib/epitools/permutations.rb in epitools-0.5.105

- old
+ new

@@ -1,11 +1,11 @@ require 'epitools' class Array - + alias_method :mult, :"*" - + # # Overloaded * operator. # # Original behaviour: # array * number == <number> copies of array @@ -24,40 +24,40 @@ result else send(:mult, other) end end - + # # Multiply the array by itself 'exponent'-times. # def **(exponent) ([self] * exponent).foldl(:*) end - + def all_pairs(reflexive=false) (0...size).each do |a| start = reflexive ? a : a+1 (start...size).each do |b| yield self[a], self[b] end end end - + enumerable :all_pairs - + end # # Returns all the `size`-sized selections of the elements from an array. # # I can't remember why I wrote it like this, but the array you want to # permute is passed in as a block. For example: # # >> perms(1) { [1,2,3,4] } -# => [[1], [2], [3], [4]] +# => [[1], [2], [3], [4]] # >> perms(2) { [1,2,3,4] } # => [[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4], # [3, 1], [3, 2], [3, 3], [3, 4], [4, 1], [4, 2], [4, 3], [4, 4]] # # The block also gets passed a parameter: the depth of the recursion. @@ -66,17 +66,17 @@ def perms(size, n=0, stack=[], &block) ps = yield(n) results = [] if n >= size results << stack - else + else ps.each do |p| results += perms(size, n+1, stack + [p], &block) end end results end - + if $0 == __FILE__ puts "-------------- foldl ---" p [:sum, [1,1,1].foldl(:+)] p ["[[1,2],[3]].foldl(:+)", [[1,2],[3]].foldl(:+)]