I see that enumerators have the methods rewind and next.
Are there equivalent methods “end” and “prev”?
I see reverse_each … but what if I do have a large enumerable object
and don’t want the intermediate array created?
The documentation (
Module: Enumerable (Ruby 1.9.3) )
for reverse each says:
reverse_each(*args) {|item| block } ? enum click to toggle source
reverse_each(*args) ? an_enumerator
Builds a temporary array and traverses that array in reverse order.
If no block is given, an enumerator is returned instead.
Enumerators can only “move” in one direction - forward. #end would be
useless for many (for ex. (1…Float::INFINITY).each - this one never
ends), providing #prev would be equivalent to remembering all of
values yielded so far (unpractical, we don’t have infinite memory).
– Matma R.
2012/3/12 Ralph S. [email protected]:
Bartosz,
The convention is to bottom post. I am moving your comment to the right
place and responding.
Monday, March 12, 2012, 11:51:54 AM, you wrote:
BD> 2012/3/12 Ralph S. [email protected]:
reverse_each(*args) {|item| block } ? enum click to toggle source
reverse_each(*args) ? an_enumerator
Builds a temporary array and traverses that array in reverse order.
If no block is given, an enumerator is returned instead.
BD> Enumerators can only “move” in one direction - forward. #end would
be
BD> useless for many (for ex. (1…Float::INFINITY).each - this one never
BD> ends), providing #prev would be equivalent to remembering all of
BD> values yielded so far (unpractical, we don’t have infinite memory).
BD> – Matma R.
For ranges, all you need define is the inverse of the succ function
(which must be defined in order to “go forwards”).
By the way,
(99_999_999_990…100_000_000_000).last(3) works just fine but
(1…100_000_000_000) seems to hang in a loop. I suspect Ruby is
constructing a temporary array.
Ruby seems not to be clever at all about ranges.
Ruby seems not to be clever at all about ranges.
If you are cleverer, suggest a solution in C to this problem?