Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > enumerator problem in 1.9.1

Reply
Thread Tools

enumerator problem in 1.9.1

 
 
Bug Free
Guest
Posts: n/a
 
      05-31-2010
The following line:

[5, 7].each_with_index.each_cons(2) {|v| p v }

prints [5, 7] but I'm expecting [[5, 0], [7, 1]].

Does anyone know how/why this is happening ?

thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Caleb Clausen
Guest
Posts: n/a
 
      05-31-2010
On 5/30/10, Bug Free <> wrote:
> The following line:
>
> [5, 7].each_with_index.each_cons(2) {|v| p v }
>
> prints [5, 7] but I'm expecting [[5, 0], [7, 1]].
>
> Does anyone know how/why this is happening ?


Works for me in 1.9.3. Maybe a 1.9.1 bug?

 
Reply With Quote
 
 
 
 
botp
Guest
Posts: n/a
 
      05-31-2010
On Mon, May 31, 2010 at 9:04 AM, Bug Free <> wrote:
> The following line:
>
> =A0 =A0[5, 7].each_with_index.each_cons(2) {|v| p v }
>
> prints [5, 7] but I'm expecting [[5, 0], [7, 1]].


you'll have specify the index, eg,

>> [5, 7].map.with_index{|v,i| [v,i]}

=3D> [[5, 0], [7, 1]]

kind regards -botp

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-31-2010
2010/5/31 botp <>:
> On Mon, May 31, 2010 at 9:04 AM, Bug Free <> wrote:
>> The following line:
>>
>> =A0 =A0[5, 7].each_with_index.each_cons(2) {|v| p v }
>>
>> prints [5, 7] but I'm expecting [[5, 0], [7, 1]].

>
> you'll have specify the index, eg,
>
>>> =A0[5, 7].map.with_index{|v,i| [v,i]}

> =3D> [[5, 0], [7, 1]]


That's not the proper replacement with map. Rather you'd do:

irb(main):009:0> [5, 7].each_with_index.map {|v| v }
=3D> [5, 7]
irb(main):010:0> [5, 7].each_with_index.map {|*v| v }
=3D> [[5, 0], [7, 1]]

Interestingly the splat operator does not include the index with #each_cons=
:

irb(main):011:0> [5, 7].each_with_index.each_cons(2) {|v| p v }
[5, 7]
=3D> nil
irb(main):012:0> [5, 7].each_with_index.each_cons(2) {|*v| p v }
[[5, 7]]
=3D> nil

This is on ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-cygwin].

I'd say it's a bug which seems to be reflected by the fact that it
works as expected in 1.9.3 according to Caleb.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      05-31-2010
On Mon, May 31, 2010 at 10:34 AM, Robert Klemme
<> wrote:
> I'd say it's a bug which seems to be reflected by the fact that it
> works as expected in 1.9.3 according to Caleb.

Even more so, as
[5, 7].each_with_index.to_a.each_cons(2) {|v| p v }
[[5, 0], [7, 1]]

Seems quite straight forward to me that the to_a should not change the
semantics.
R.

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      05-31-2010
On Mon, May 31, 2010 at 4:34 PM, Robert Klemme
<> wrote:
> 2010/5/31 botp <>:
>> On Mon, May 31, 2010 at 9:04 AM, Bug Free <> wrote:
>>> The following line:
>>>
>>> =A0 =A0[5, 7].each_with_index.each_cons(2) {|v| p v }
>>>
>>> prints [5, 7] but I'm expecting [[5, 0], [7, 1]].

>>
>> you'll have specify the index, eg,
>>
>>>> =A0[5, 7].map.with_index{|v,i| [v,i]}

>> =3D> [[5, 0], [7, 1]]

>
> That's not the proper replacement with map. =A0Rather you'd do:
>
> irb(main):009:0> [5, 7].each_with_index.map {|v| v }
> =3D> [5, 7]
> irb(main):010:0> [5, 7].each_with_index.map {|*v| v }
> =3D> [[5, 0], [7, 1]]


brain dead here.
can you enlighten me on what is "not proper" ?

ie why is each_with_index.map preferable over map.with_index ?

kind regards -botp

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-31-2010
2010/5/31 botp <>:
> On Mon, May 31, 2010 at 4:34 PM, Robert Klemme
> <> wrote:
>> 2010/5/31 botp <>:
>>> On Mon, May 31, 2010 at 9:04 AM, Bug Free <> wrote:
>>>> The following line:
>>>>
>>>> =A0 =A0[5, 7].each_with_index.each_cons(2) {|v| p v }
>>>>
>>>> prints [5, 7] but I'm expecting [[5, 0], [7, 1]].
>>>
>>> you'll have specify the index, eg,
>>>
>>>>> =A0[5, 7].map.with_index{|v,i| [v,i]}
>>> =3D> [[5, 0], [7, 1]]

>>
>> That's not the proper replacement with map. =A0Rather you'd do:
>>
>> irb(main):009:0> [5, 7].each_with_index.map {|v| v }
>> =3D> [5, 7]
>> irb(main):010:0> [5, 7].each_with_index.map {|*v| v }
>> =3D> [[5, 0], [7, 1]]

>
> brain dead here.
> can you enlighten me on what is "not proper" ? =A0
>
> ie why is each_with_index.map preferable over map.with_index ?


Because it is closer on what OP initially did:

[5, 7].each_with_index.each_cons(2) {|v| p v }

The sequence "[5, 7].each_with_index" returns an Enumerator which must
yield a value and an index on each iteration:

irb(main):001:0> [5, 7].each_with_index.each {|*a| p a}
[5, 0]
[7, 1]
=3D> [5, 7]

On this Enumerator which #each_cons is invoked.

Contrast that to your suggestion "[5, 7].map.with_index{|v,i| [v,i]}":
here "[5, 7].map" returns an Enumerator which must yield values *only*
- the index is added by "#map_with_index".

irb(main):002:0> [5, 7].map.each {|*a| p a}
[5]
[7]
=3D> [[5], [7]]

This means that in your code the index cannot get lost along the way
simply because it is added in the last step and not in the initial
step. Hope it's clearer now.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Bug Free
Guest
Posts: n/a
 
      05-31-2010
Robert Dober wrote:
> On Mon, May 31, 2010 at 10:34 AM, Robert Klemme
> <> wrote:
>> I'd say it's a bug which seems to be reflected by the fact that it
>> works as expected in 1.9.3 according to Caleb.

> Even more so, as
> [5, 7].each_with_index.to_a.each_cons(2) {|v| p v }
> [[5, 0], [7, 1]]
>
> Seems quite straight forward to me that the to_a should not change the
> semantics.
> R.


Thanks for all the responses. Where do I pick up 1.9.3 ? I don't see it
at ftp://ftp.ruby-lang.org/pub/ruby/1.9

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bug Free
Guest
Posts: n/a
 
      05-31-2010
Robert Dober wrote:
> On Mon, May 31, 2010 at 10:34 AM, Robert Klemme
> <> wrote:
>> I'd say it's a bug which seems to be reflected by the fact that it
>> works as expected in 1.9.3 according to Caleb.

> Even more so, as
> [5, 7].each_with_index.to_a.each_cons(2) {|v| p v }
> [[5, 0], [7, 1]]
>
> Seems quite straight forward to me that the to_a should not change the
> semantics.
> R.


Just checked the just-released 1.9.2-preview3 and it too seems to have
the fix.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      06-01-2010
On Mon, May 31, 2010 at 9:53 PM, Robert Klemme
<> wrote:
> Because it is closer on what OP initially did:


LOL. ok i get it
i was just pointing out how to retrieve the index (and the relevance
of it). i wouldn't want to "give" him *the* answer because i think
rubyist _are_ programmers...

kind regards -botp

> irb(main):001:0> [5, 7].each_with_index.each {|*a| p a}
> [5, 0]
> [7, 1]
> => [5, 7]


or simply,

=> [5, 7].each_with_index{|*a| p a}
[5, 0]
[7, 1]
=> [5, 7]


> This means that in your code the index cannot get lost along the way
> simply because it is added in the last step and not in the initial
> step.


shouldn't the programmer be the judge for that?

eg, i can easily scale if i need the square and the index...

> [5, 7].map{|a| a*a }.each_with_index{|*a| p a}

[25, 0]
[49, 1]

> Kind regards
> robert


best regards
-botp

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does the first enumerator have default value of 0 like in C++? Thomas Hawtin Java 7 05-20-2006 06:26 PM
Nvidia Network Bus enumerator Richard G Carruthers Windows 64bit 3 06-03-2005 05:23 PM
Hijackthis freezes on 015 Trusted Zone enumerator geermeister@gmail.com Computer Support 0 04-29-2005 03:30 PM
DataGrid: Bind to an Enumerator of object[]'s Angelos Karantzalis ASP .Net 1 11-05-2004 09:04 AM
Char Enumerator =?Utf-8?B?SmltIEhlYXZleQ==?= ASP .Net 1 07-16-2004 10:36 PM



Advertisments