Module: Combinatorics::Permute::Mixin

Included in:
Array, Set
Defined in:
lib/combinatorics/permute/mixin.rb

Overview

Author:

Since:

  • 0.4.0

Instance Method Summary collapse

Instance Method Details

#permute(r) {|permutation| ... } ⇒ Enumerator

Enumerate distinct r-permutations for a particular sequence of elements.

Examples:

[1, 2, 3].permute(2).to_a
# => [[1, 2], [1, 3],
#     [2, 1], [2, 3],
#     [3, 1], [3, 2]]

Parameters:

  • r (Fixnum)

    Length of permuted subsets to return.

Yields:

  • (permutation)

    If a block is given, it will be passed each k-permutation.

Yield Parameters:

  • permutation (Array)

    A k-permutation of the elements from self.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator of the k-permutations of elements from self is returned.

Raises:

  • (TypeError)

    self must be Enumerable.

See Also:

Since:

  • 0.4.0



39
40
41
42
43
44
45
46
47
# File 'lib/combinatorics/permute/mixin.rb', line 39

def permute(r,&block)
  return enum_for(:permute,r) unless block

  unless kind_of?(Enumerable)
    raise(TypeError,"#{inspect} must be Enumerable")
  end

  self.to_a.permutation(r,&block)
end