Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Find index of next match in array, or find with offset

Tags:

arrays

ruby

I want to find further matches after Array#find_index { |item| block } matches for the first time. How can I search for the index of the second match, third match, and so on?

In other words, I want the equivalent of the pos argument to Regexp#match(str, pos) for Array#find_index. Then I can maintain a current-position index to continue the search.

I cannot use Enumerable#find_all because I might modify the array between calls (in which case, I will also adjust my current-position index to reflect the modifications). I do not want to copy part of the array, as that would increase the computational complexity of my algorithm. I want to do this without copying the array:

new_pos = pos + array[pos..-1].find_index do |elem|
    elem.matches_condition?
end

The following are different questions. They only ask the first match in the array, plus one:

  • https://stackoverflow.com/questions/11300886/ruby-how-to-find-the-next-match-in-an-array
  • https://stackoverflow.com/questions/4596517/ruby-find-next-in-array

The following question is closer, but still does not help me, because I need to process the first match before continuing to the next (and this way also conflicts with modification):

  • https://stackoverflow.com/questions/9925654/ruby-find-in-array-with-offset
like image 758
martinjs Avatar asked Nov 30 '15 12:11

martinjs


2 Answers

A simpler way to do it is just:

new_pos = pos
while new_pos < array.size and not array[new_pos].matches_condition?
    new_pos += 1
end
new_pos = nil if new_pos == array.size

In fact, I think this is probably better than my other answer, because it's harder to get wrong, and there's no chance of future shadowing problems being introduced from the surrounding code. However, it's still clumsy.

And if the condition is more complex, then you end up needing to do something like this:

new_pos = pos
# this check is only necessary if pos may be == array.size
if new_pos < array.size
    prepare_for_condition
end
while new_pos < array.size and not array[new_pos].matches_condition?
    new_pos += 1
    if new_pos < array.size
        prepare_for_condition
    end
end
new_pos = nil if new_pos == array.size

Or, God forbid, a begin ... end while loop (although then you run into trouble with the initial value of new_pos):

new_pos = pos - 1
begin
    new_pos += 1
    if new_pos < array.size
        prepare_for_condition
    end
end while new_pos < array.size and not array[new_pos].matches_condition?
new_pos = nil if new_pos == array.size

This may seem horrible. However, supposing prepare_for_condition is something that keeps being tweaked in small ways. Those tweaks will eventually get refactored; however, by that time, the output of the refactored code will also end up getting tweaked in small ways that don't belong with the old refactored code, but do not yet seem to justify refactoring of their own - and so on. Occasionally, someone will forget to change both places. This may seem pathological; however, in programming, as we all know, the pathological case has a habit of occurring only too often.

like image 106
martinjs Avatar answered Sep 26 '22 05:09

martinjs


Here is one way this can be done. We can define a new method in Array class that will allow us to find indexes that match a given condition. The condition can be specified as block that returns boolean.

The new method returns an Enumerator so that we get the benefit of many of the Enumerator methods such next, to_a, etc.

ary = [1,2,3,4,5,6]

class Array
  def find_index_r(&block)
    Enumerator.new do |yielder| 
      self.each_with_index{|i, j| yielder.yield j if block.call(i)}
    end
  end
end

e = ary.find_index_r { |r| r % 2 == 0 }

p e.to_a  #=> [1, 3, 5]
p e.next  
#=> 1
p e.next  
#=> 3
ary[2]=10 
p ary
#=> [1, 2, 10, 4, 5, 6]
p e.next  
#=> 5
e.rewind
p e.next  
#=> 1
p e.next  
#=> 2

Note: I added a new method in Array class for demonstration purpose. Solution can be adapted easily to work without the monkey-patching

like image 20
Wand Maker Avatar answered Sep 25 '22 05:09

Wand Maker