Why Enumerator#next does not return more than one value?

If I have an Enumerator which yields elements of a mathematical series
(logistic map), I can use:

enum.first(10) to get the first 10 value,
enum.next to get the next values,

but there is no enum.next(10) to get the next 10 values.
I do some trickery like this to achieve the same result (because I need
the result in an Array, as #first returns it):
(0…20).map {(lm.next)}

Is there a nicer way to do it? (Or did I overlook some existing method
in Enumerable?)

At first I thought it is logical that #next should have a parameter so
the #first(10) method could be actually a #rewind + #next(10).

Hi,

Ruby doesn’t seem to have this feature by default. But why not simply
change the next method yourself?

On Mon, May 7, 2012 at 3:10 PM, Fldes L. [email protected] wrote:

Is there a nicer way to do it? (Or did I overlook some existing method
in Enumerable?)

Question is: what do you need that for?

At first I thought it is logical that #next should have a parameter so
the #first(10) method could be actually a #rewind + #next(10).

If you need chunks of the same size #each_slice is the proper method to
use.

irb(main):015:0> 10.times.each_slice(3).to_a
=> [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

Note: there’s also #each_cons

irb(main):017:0> 10.times.each_cons(7).to_a
=> [[0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7,
8], [3, 4, 5, 6, 7, 8, 9]]

Kind regards

robert

On May 7, 2012, at 3:10 PM, Fldes L. wrote:

Is there a nicer way to do it? (Or did I overlook some existing method
in Enumerable?)

At first I thought it is logical that #next should have a parameter so
the #first(10) method could be actually a #rewind + #next(10).

This is what #take does:

has an example in the docs of #new.

Florian G. wrote in post #1059865:

This is what #take does:

Class: Enumerator (Ruby 1.9.3)

has an example in the docs of #new.

By checking the documentation and making some small tests there is no
difference between #take and
#first. Both rewind and return some values from the start.

Robert K. wrote in post #1059863:

On Mon, May 7, 2012 at 3:10 PM, Fldes L. [email protected] wrote:

Is there a nicer way to do it? (Or did I overlook some existing method
in Enumerable?)

Question is: what do you need that for?

I’m trying to find some pattern in a certain series (a logistic map).
The series is infinite.
I want to get - say- 10 values from the Enumerator, examine it, and
either return something (“found”) and/or continue searching. (I know
this
won’t find overlaps, it’s just an experiment).

Both #each_slice and #each_cons try to build an Array from the
enumerator, which is - in this case - impossible, because the Enumerator
is infinite and would eat up all the memory.

I read about lazy_select and chained filters for enum which might be a
solution but I have yet to understand them. For the time being I’m
sticking to that #map solution.

Thanks

Robert K. wrote in post #1059915:

There are other ways though. Did you see my posting?

Yes, I just accidentally replied them in the wrong order.

On 07.05.2012 23:24, Földes László wrote:

Both #each_slice and #each_cons try to build an Array from the
enumerator, which is - in this case - impossible, because the Enumerator
is infinite and would eat up all the memory.

That’s not true. Both each_slice and each_cons return enumerators
(or yield directly to the block when given one) and it’s perfectly
possible to use them with infinite sequences.

On Mon, May 7, 2012 at 10:37 PM, Fldes L. [email protected]
wrote:

Florian G. wrote in post #1059865:

This is what #take does:

Class: Enumerator (Ruby 1.9.3)

has an example in the docs of #new.

By checking the documentation there is no difference between #take and
#first. Both rewind and return some values from the start.

There are other ways though. Did you see my posting?

Cheers

robert

Sebastian H. wrote in post #1059935:

On 07.05.2012 23:24, Földes László wrote:

Both #each_slice and #each_cons try to build an Array from the
enumerator, which is - in this case - impossible, because the Enumerator
is infinite and would eat up all the memory.

That’s not true. Both each_slice and each_cons return enumerators
(or yield directly to the block when given one) and it’s perfectly
possible to use them with infinite sequences.

True! Don’t know why I thought the opposite.

Thank you, this solved my problem.