From: Magnus Holm Date: 2013-08-31T11:59:57+02:00 Subject: [ruby-core:56931] Re: [ruby-trunk - Feature #8840][Open] Yielder#state --f46d042c65a7224d5904e53b680a Content-Type: text/plain; charset=UTF-8 On Sat, Aug 31, 2013 at 12:52 AM, marcandre (Marc-Andre Lafortune) < ruby-core@marc-andre.ca> wrote: > Defining an Enumerator that require a state is currently troublesome. For > example, it is not really possible to define an equivalent of Lazy#drop in > Ruby without making an assumption on the implementation. > Can't you just use the closure? > class Enumerator::Lazy < Enumerator > def drop(n) > n = n.to_i > Lazy.new(self) do |yielder, *values| > if n > 0 > n -= 1 > else > yielder.yield(*values) > end > end > end > end > --f46d042c65a7224d5904e53b680a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Sat, Aug 31, 2013 at 12:52 AM, marcandre (Marc-Andre La= fortune) <ruby-core@marc-andre.ca> wrote:
Defining an Enumerator that require a state is currently troublesome. For e= xample, it is not really possible to define an equivalent of Lazy#drop in R= uby without making an assumption on the implementation.

Can't you just use the closure?
=C2=A0
=C2=A0 class Enumerator::Lazy < Enumerator
=C2=A0 =C2=A0 def drop(n)
=C2=A0 =C2=A0 =C2=A0 n =3D n.to_i
=C2=A0 =C2=A0 =C2=A0 Lazy.new(self) do |yielder, *values|
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 if n > 0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 n -=3D 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 yielder.yield(*values)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 end
=C2=A0 end

--f46d042c65a7224d5904e53b680a--