Skip to content

Commit

Permalink
1.9.2: Implement Array#repeated_combination, #repeated_permutation (v…
Browse files Browse the repository at this point in the history
… 1.17.0)
  • Loading branch information
marcandre committed May 18, 2010
1 parent 9c59af0 commit acd0b6a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rdoc
@@ -1,8 +1,14 @@
= Packable --- History

== Version 1.17.0 - May 17th, 2010

* Added some features of 1.9.2:

* Array#repeated_combination, #repeated_permutation

== Version 1.16.2 - April 11th, 2010

* Added a features of 1.9.2:
* Added some features of 1.9.2:

* Array#uniq, #uniq! (with block)
* Array#product (with block)
Expand Down
1 change: 1 addition & 0 deletions README.rdoc
Expand Up @@ -110,6 +110,7 @@ but since it is only an imitation, it must be required explicitly:
* <tt>rotate, rotate!</tt>
* <tt>keep_if, select!</tt>
* +product+ (with block)
* +repeated_combination+, +repeated_permutation+
* <tt>sort_by!</tt>
* <tt>uniq, #uniq!</tt> (with block)

Expand Down
4 changes: 2 additions & 2 deletions VERSION.yml
@@ -1,5 +1,5 @@
---
:major: 1
:minor: 17
:patch: 0
:build:
:minor: 16
:patch: 7
4 changes: 2 additions & 2 deletions backports.gemspec
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{backports}
s.version = "1.16.7"
s.version = "1.17.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Marc-Andr\303\251 Lafortune"]
s.date = %q{2010-05-03}
s.date = %q{2010-05-17}
s.description = %q{ Essential backports that enable some of the really nice features of ruby 1.8.7, ruby 1.9 and rails from ruby 1.8.6 and earlier.
}
s.email = %q{github@marc-andre.ca}
Expand Down
37 changes: 37 additions & 0 deletions lib/backports/1.9.2/array.rb
Expand Up @@ -27,6 +27,43 @@ def product_with_block(*arg, &block)
Backports.alias_method_chain self, :product, :block
end

# Note: Combinations are not yielded in the same order as MRI.
# This is not a bug; the spec states that the order is implementation dependent
def repeated_combination(num, &block)
return to_enum :repeated_combination, num unless block_given?
num = Backports.coerce_to_int(num)
if num <= 0
yield [] if num == 0
else
indices = Array.new(num, 0)
indices[-1] = size
while dec = indices.find_index(&:nonzero?)
indices[0..dec] = Array.new dec+1, indices[dec]-1
yield values_at(*indices)
end
end
self
end unless method_defined? :repeated_combination

# Note: Permutations are not yielded in the same order as MRI.
# This is not a bug; the spec states that the order is implementation dependent
def repeated_permutation(num, &block)
return to_enum :repeated_permutation, num unless block_given?
num = Backports.coerce_to_int(num)
if num <= 0
yield [] if num == 0
else
indices = Array.new(num, 0)
indices[-1] = size
while dec = indices.find_index(&:nonzero?)
indices[0...dec] = Array.new dec, size-1
indices[dec] -= 1
yield values_at(*indices)
end
end
self
end unless method_defined? :repeated_permutation

def rotate(n=1)
dup.rotate!(n)
end unless method_defined? :rotate
Expand Down

0 comments on commit acd0b6a

Please sign in to comment.