Permalink
Browse files

add Array#permutations:

  • Loading branch information...
bakkdoor committed Nov 15, 2013
1 parent 644b7a0 commit af2a438131a757f90d8fd7e805fbe0caf5ff1078
Showing with 28 additions and 0 deletions.
  1. +16 −0 lib/rbx/array.fy
  2. +3 −0 lib/rbx/enumerator.fy
  3. +9 −0 tests/array.fy
@@ -139,4 +139,20 @@ class Array {
unshift(value)
}
def permutations: size (self size) {
"""
@size Optional size of permutations to be returned. Defaults to @self's size.
@return @Enumerable::Enumerator@ of all permutations of @self.
Example:
[1,2,3] permutations to_a
# => [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
[1,2,3] permutations: 2 . to_a
# => [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
"""
to_enum('permutation, size)
}
}
@@ -1,4 +1,7 @@
class Enumerable Enumerator {
forwards_unary_ruby_methods
alias_method: 'to_a for_ruby: 'to_a
def each: block {
each(&block)
}
@@ -570,4 +570,13 @@ FancySpec describe: Array with: {
[0,1,2,3] from: 0 to: 3 . is: [0,1,2,3]
[0,1,2,3] from: -1 to: 3 . is: [3]
}
it: "returns an array of permutations of self" with: 'permutations: when: {
[] permutations to_a is: [[]]
[] permutations: 0 . to_a is: [[]]
[1] permutations to_a is: [[1]]
[1,2] permutations to_a is: [[1,2], [2,1]]
[1,2,3] permutations to_a is: \
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
}
}

0 comments on commit af2a438

Please sign in to comment.