This is the essence of what I'm trying to do, but the “break” does not work correctly:
needle = nil
haystacks.each do |haystack|
needle = haystack.look_for_needle()
break if needle
end
This is shorter, but it will iterate over all the piles of hay (without looking, at least), even if it is not needed:
needle = nil
haystacks.each do |haystack|
needle ||= haystack.look_for_needle()
end
This is single-line, but (I believe) it is not lazy and therefore does unnecessary work:
needle = hackstacks.map{|h| h.look_for_needle()}.detect{|x| !x.nil?}
I feel that there should be one liner, but I'm not sure what it will be:
needle = hackstacks.some_find_each_first_detect_lazy_map_thingee {|h| h.look_for_needle()}
source
share