Module: ConditionalSample::MixMe

Defined in:
lib/conditional_sample/public.rb,
lib/conditional_sample/private.rb

Overview

This module is suitable as a mixin, using the results of self#to_a

It is automatically included in Array, so each of these methods are added to Array when you require ‘conditional_sample’

For both methods, the ‘conditions’ array must contain boolean procs using args |arr, elem|

arr

a reference to the current array that has been built up through the recursion chain.

elem

a reference to the current element being considered.

Instance Method Summary collapse

Instance Method Details

#conditional_permutation(conditions, seconds = nil) ⇒ Object

Return a permutation of ‘array’ where each element validates to the same index in a ‘conditions’ array of procs that return Boolean.

The output is an array that is a complete permutation of the input array. i.e. output.length == array.length

Any elements in the array that are extra to the number of conditions will be assumed valid.

array = [1,2,3,4,5].shuffle
conditions = [
  proc { |arr, elem| elem < 2},
  proc { |arr, elem| elem > 2},
  proc { |arr, elem| elem > 1}
]
array.conditional_permutation(conditions)

possible output => [1, 3, 4, 5, 2]

Will not work on arrays that contain nil values.



42
43
44
45
46
# File 'lib/conditional_sample/public.rb', line 42

def conditional_permutation conditions, seconds = nil
  timeout_rescue(seconds, []) do
    conditional_permutation_recurse(self.to_a, conditions)
  end
end

#conditional_sample(conditions, seconds = nil) ⇒ Object

Return values from ‘array’ where each element validates to the same index in a ‘conditions’ array of procs that return Boolean.

The output is an array of conditions.length that is a partial permutation of the input array, where satisfies only the conditions.

Any elements in the array that are extra to the number of conditions will not be output.

array = [1,2,3,4,5].shuffle
conditions = [
  proc { |arr, elem| elem < 2},
  proc { |arr, elem| elem > 2},
  proc { |arr, elem| elem > 1}
]
array.conditional_sample(conditions)

possible output => [1, 5, 3]

Will not work on arrays that contain nil values.



70
71
72
73
74
# File 'lib/conditional_sample/public.rb', line 70

def conditional_sample conditions, seconds = nil
  timeout_rescue(seconds, []) do
    conditional_sample_recurse(self.to_a, conditions)
  end
end