[DOC] Tweaks for Array#repeated_permutation (#11873)

This commit is contained in:
Burdette Lamar 2024-10-11 10:12:12 -05:00 committed by GitHub
parent a9fb0a2083
commit c51947671e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
Notes: git 2024-10-11 15:12:31 +00:00
Merged-By: peterzhu2118 <peter@peterzhu.ca>

71
array.c
View File

@ -7213,68 +7213,41 @@ rb_ary_repeated_permutation_size(VALUE ary, VALUE args, VALUE eobj)
/*
* call-seq:
* array.repeated_permutation(n) {|permutation| ... } -> self
* array.repeated_permutation(n) -> new_enumerator
* repeated_permutation(size) {|permutation| ... } -> self
* repeated_permutation(size) -> new_enumerator
*
* Calls the block with each repeated permutation of length +n+ of the elements of +self+;
* each permutation is an +Array+;
* With a block given, calls the block with each repeated permutation of length +size+
* of the elements of +self+;
* each permutation is an array;
* returns +self+. The order of the permutations is indeterminate.
*
* When a block and a positive Integer argument +n+ are given, calls the block with each
* +n+-tuple repeated permutation of the elements of +self+.
* The number of permutations is <tt>self.size**n</tt>.
* If a positive integer argument +size+ is given,
* calls the block with each +size+-tuple repeated permutation of the elements of +self+.
* The number of permutations is <tt>self.size**size</tt>.
*
* +n+ = 1:
* Examples:
*
* a = [0, 1, 2]
* a.repeated_permutation(1) {|permutation| p permutation }
* - +size+ is 1:
*
* Output:
* p = []
* [0, 1, 2].repeated_permutation(1) {|permutation| p.push(permutation) }
* p # => [[0], [1], [2]]
*
* [0]
* [1]
* [2]
* - +size+ is 2:
*
* +n+ = 2:
* p = []
* [0, 1, 2].repeated_permutation(2) {|permutation| p.push(permutation) }
* p # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
*
* a.repeated_permutation(2) {|permutation| p permutation }
* If +size+ is zero, calls the block once with an empty array.
*
* Output:
* If +size+ is negative, does not call the block:
*
* [0, 0]
* [0, 1]
* [0, 2]
* [1, 0]
* [1, 1]
* [1, 2]
* [2, 0]
* [2, 1]
* [2, 2]
* [0, 1, 2].repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
*
* If +n+ is zero, calls the block once with an empty +Array+.
*
* If +n+ is negative, does not call the block:
*
* a.repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
*
* Returns a new Enumerator if no block given:
*
* a = [0, 1, 2]
* a.repeated_permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
*
* Using Enumerators, it's convenient to show the permutations and counts
* for some values of +n+:
*
* e = a.repeated_permutation(0)
* e.size # => 1
* e.to_a # => [[]]
* e = a.repeated_permutation(1)
* e.size # => 3
* e.to_a # => [[0], [1], [2]]
* e = a.repeated_permutation(2)
* e.size # => 9
* e.to_a # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
* With no block given, returns a new Enumerator.
*
* Related: see {Methods for Combining}[rdoc-ref:Array@Methods+for+Combining].
*/
static VALUE
rb_ary_repeated_permutation(VALUE ary, VALUE num)