Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/inochi/util/combo.rb

Overview

– Copyright protects this work. See LICENSE file for details. ++

Instance Method Summary collapse

Instance Method Details

#combination(sample_size = self.length, &sampler) ⇒ Object

Returns all possible combinations made from sample_size number of items from this list.

Parameters

sample_size

The length of each combination.

sampler

If given, each combination is passed to this block.



70
71
72
# File 'lib/inochi/util/combo.rb', line 70

def combination(sample_size = self.length, &sampler)
  pnk_cnk_impl(sample_size, true, &sampler)
end

#combinations(&sampler) ⇒ Object

Returns all possible combinations of all possible lengths.

Parameters

sampler

If given, each combination is passed to this block.



84
85
86
# File 'lib/inochi/util/combo.rb', line 84

def combinations &sampler
  all_lengths_impl :combination, &sampler
end

#enumeration(sample_size = self.length, &sampler) ⇒ Object

Returns all possible enumerations made from sample_size number of items from this list.

Parameters

sample_size

The length of each enumeration.

sampler

If given, each enumeration is passed to this block.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/inochi/util/combo.rb', line 20

def enumeration(sample_size = self.length, &sampler)
  return [] if sample_size < 1

  results = []

  visitor = lambda do |parents|
    each do |child|
      result = parents + [child]

      if result.length < sample_size
        visitor.call result
      else
        yield result if block_given?
        results << result
      end
    end
  end

  visitor.call []
  results
end

#enumerations(&sampler) ⇒ Object

Returns all possible enumerations of all possible lengths.

Parameters

sampler

If given, each enumeration is passed to this block.



52
53
54
# File 'lib/inochi/util/combo.rb', line 52

def enumerations &sampler
  all_lengths_impl :enumeration, &sampler
end

#permutation(sample_size = self.length, &sampler) ⇒ Object

Returns all possible permutations made from sample_size number of items from this list.

Parameters

sample_size

The length of each permutation.

sampler

If given, each permutation is passed to this block.



102
103
104
# File 'lib/inochi/util/combo.rb', line 102

def permutation(sample_size = self.length, &sampler)
  pnk_cnk_impl(sample_size, false, &sampler)
end

#permutations(&sampler) ⇒ Object

Returns all possible permutations of all possible lengths.

Parameters

sampler

If given, each permutation is passed to this block.



116
117
118
# File 'lib/inochi/util/combo.rb', line 116

def permutations &sampler
  all_lengths_impl :permutation, &sampler
end