From: Tanaka Akira Date: 2013-10-07T23:55:58+09:00 Subject: [ruby-core:57701] Re: [ruby-trunk - Feature #8840] Yielder#state 2013/10/3 marcandre (Marc-Andre Lafortune) : > Issue #8840 has been updated by marcandre (Marc-Andre Lafortune). > > > akr (Akira Tanaka) wrote: >> Would you explain the incompleteness concretely? > > Sure. With your code above: > > e.drop2(40).map(&:odd?) # => [true, false] > # expected lazy enumerator, as with original drop: > e.drop(40).map(&:odd?) # => #:drop(40)>:map> > > Here is another implementation using `with_state` that returns a lazy enumerator: > > class Enumerator::Lazy > def drop3(n) > Lazy.new(with_state(remain: n)) do |y, (state, v)| > if state[:remain] == 0 > y.yield v > else > state[:remain] -= 1 > end > end > end > end > > This implementation doesn't look so bad. It's probably quite a bit slower than using a Yielder#state method though. Thank you. I understand. I still like with_state than Yielder#state because it limits stateful behaviors into a method. -- Tanaka Akira