From: Joey Date: 2006-09-28T07:13:44+09:00 Subject: Re: Injecting truth ------=_Part_29488_10153643.1159395221152 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hmm, why Enumerable#all? without short circuit? If you get to a point where the block doesnt apply, then you might aswell break. j`ey http://www.eachmapinject.com On 9/27/06, Ken Kunz wrote: > > I had a scenario arise today where I needed an iterator with similar > semantics as Enumerable#all?, but non-short-circuiting. We came up with > a fairly elegant solution (imho), so I thought I'd be a good rubyzen > and share :) > > 1) First... the standard #all? (short-circuits): > > irb> (1..5).all? { |i| puts i; i < 3 } > 1 > 2 > 3 > => false > > 2) Non-short-circuiting version (injecting truth): > > irb> (1..5).inject(true) { |retval, i| puts i; retval &&= (i < 3) } > 1 > 2 > 3 > 4 > 5 > => false > > 3) One more way (though I like #2 better) > > irb(main):016:0> (1..5).partition { |i| puts i; i < 3 }.last.empty? > 1 > 2 > 3 > 4 > 5 > => false > > -Ken > > > ------=_Part_29488_10153643.1159395221152--