Hi Adriel,
What's the data structure in ruby for Iterator?
What I meant is ,the Iterator is lazy and presents only one at each time.
The equivalent to a Scala / Java / GoF *Iterator* or a .NET
*Enumerator* in Ruby is the `Enumerator` class
(class Enumerator - RDoc Documentation).
An `Enumerator` yields values one-by-one.
Many methods that iterate over a collection will return an
`Enumerator` when you call them without a block, e.g.
`Enumerable#map`.
Note that while `Enumerator` mixes in `Enumerable` and thus provides
all the methods you are used to, all of these methods are *strict*
(i.e. they iterate over the whole `Enumerator`) and they return
`Array`s. So, they are not useful for infinite or very large
`Enumerator`s.
That's what `Enumerator::Lazy`
(class Enumerator::Lazy - RDoc Documentation) is for: it overrides
many of the methods in `Enumerable` with lazy versions. You can
construct an `Enumerator::Lazy` by calling `Enumerable#lazy` on any
`Enumerable` object, including non-lazy `Enumerator`s.
(This is the default data type for file IO in scala)
I want it to read very big files and streams etc.
In Ruby, you can iterate over large files and I/O streams using
`IO::foreach` (class IO - RDoc Documentation). I
mentioned above that many iteration methods conform to the protocol
that not passing a block means you want an `Enumerator`, and
`IO::foreach` is no different. If you want an `Enumerator` which
iterates over an I/O stream line-by-line, you can use:
io_iterator = IO.foreach(some_io_stream)
And if you want that iterator to be lazy, just add the `Enumerable#lazy` method:
lazy_io_iterator = IO.foreach(some_io_stream).lazy
Cheers!
···
Adriel Peng <peng.adriel@gmail.com> wrote: