assuming I have the following array:
views = [
{ :user_id => 1, :viewed_at => '2012-06-29 17:03:28 -0400' },
{ :user_id => 1, :viewed_at => '2012-06-29 17:04:28 -0400' },
{ :user_id => 2, :viewed_at => '2012-06-29 17:05:28 -0400' },
{ :user_id => 3, :viewed_at => '2012-06-29 17:06:28 -0400' },
{ :user_id => 1, :viewed_at => '2012-06-29 17:07:28 -0400' },
{ :user_id => 1, :viewed_at => '2012-06-29 17:08:28 -0400' },
{ :user_id => 3, :viewed_at => '2012-06-29 17:09:28 -0400' },
{ :user_id => 3, :viewed_at => '2012-06-29 17:16:28 -0400' },
{ :user_id => 3, :viewed_at => '2012-06-29 17:26:28 -0400' },
{ :user_id => 3, :viewed_at => '2012-06-29 17:36:28 -0400' },
{ :user_id => 1, :viewed_at => '2012-06-29 17:47:28 -0400' },
{ :user_id => 2, :viewed_at => '2012-06-29 17:57:28 -0400' },
{ :user_id => 3, :viewed_at => '2012-06-29 17:67:28 -0400' },
{ :user_id => 1, :viewed_at => '2012-06-29 17:77:28 -0400' }
]
assuming the array is ordered by viewed_at
If I want to retrieve the last view hash in the views array for a particular user_id, I could do the following:
views.reverse.detect { |view| view[:user_id] == 1 }
where detect would returns first item in an enumerable where the block evaluates to true.
My question is: I assume there is O(n)
cost to the reverse method, so how can I detect in reverse without having to reverse the array? Or is the reverse method not O(n)
?
Method Array#reverse
is O(n) in time and space. As you don't need the whole reversed array, you may use Array#reverse_each, that would be O(1) in space. In practice, that's only relevant for really big arrays.
views.reverse_each.detect { |view| view[:user_id] == 1 }
#=> {:user_id=>1, :viewed_at=>"2012-06-29 17:77:28 -0400"}