Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Iterating list in pairs

Reply
Thread Tools

Iterating list in pairs

 
 
FireAphis
Guest
Posts: n/a
 
      08-08-2007
Hello,

I need to iterate through a list and handle two elements on every
iteration. That is I'd like to do something like

[1,2,3,4,5].each { |x, y| puts x.to_s + y.to_s }

12
23
34
45

This code doesn't work off course.
I can iterate using indices

0.upto(list.size-1) { |i| puts list[i] + list[i+1] }

But it looks ugly to me. Do you know any elegant tricks that don't use
list indices?

Thanks
FireAphis

 
Reply With Quote
 
 
 
 
dblack@rubypal.com
Guest
Posts: n/a
 
      08-08-2007
Hi --

On Wed, 8 Aug 2007, FireAphis wrote:

> Hello,
>
> I need to iterate through a list and handle two elements on every
> iteration. That is I'd like to do something like
>
> [1,2,3,4,5].each { |x, y| puts x.to_s + y.to_s }
>
> 12
> 23
> 34
> 45


[1,2,3,4,5].inject {|a,b| puts "#{a}#{b}"; b }


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      08-08-2007
FireAphis wrote:
> Hello,
>
> I need to iterate through a list and handle two elements on every
> iteration. That is I'd like to do something like
>
> [1,2,3,4,5].each { |x, y| puts x.to_s + y.to_s }
>
> 12
> 23
> 34
> 45
>
> This code doesn't work off course.
> I can iterate using indices
>
> 0.upto(list.size-1) { |i| puts list[i] + list[i+1] }
>
> But it looks ugly to me. Do you know any elegant tricks that don't use
> list indices?

irb(main):001:0> a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> a[0,4].zip(a[1,5]).each{|x,y| puts x.to_s + y.to_s}
12
23
34
45
=> [[1, 2], [2, 3], [3, 4], [4, 5]]

Unless a[0,4] breaks your "no list indices" rule, of course

--
Alex

 
Reply With Quote
 
Jano Svitok
Guest
Posts: n/a
 
      08-08-2007
On 8/8/07, FireAphis <(E-Mail Removed)> wrote:
> Hello,
>
> I need to iterate through a list and handle two elements on every
> iteration. That is I'd like to do something like
>
> [1,2,3,4,5].each { |x, y| puts x.to_s + y.to_s }
>
> 12
> 23
> 34
> 45
>
> This code doesn't work off course.
> I can iterate using indices
>
> 0.upto(list.size-1) { |i| puts list[i] + list[i+1] }
>
> But it looks ugly to me. Do you know any elegant tricks that don't use
> list indices?
>
> Thanks
> FireAphis


Enumerable#each_slice(n) {|...| ...}

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      08-08-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi --
>
> On Wed, 8 Aug 2007, FireAphis wrote:
>
>> Hello,
>>
>> I need to iterate through a list and handle two elements on every
>> iteration. That is I'd like to do something like
>>
>> [1,2,3,4,5].each { |x, y| puts x.to_s + y.to_s }
>>
>> 12
>> 23
>> 34
>> 45

>
> [1,2,3,4,5].inject {|a,b| puts "#{a}#{b}"; b }
>

Oh, that's neat

--
Alex

 
Reply With Quote
 
Jano Svitok
Guest
Posts: n/a
 
      08-08-2007
On 8/8/07, Jano Svitok <(E-Mail Removed)> wrote:
> On 8/8/07, FireAphis <(E-Mail Removed)> wrote:
> > Hello,
> >
> > I need to iterate through a list and handle two elements on every
> > iteration. That is I'd like to do something like
> >
> > [1,2,3,4,5].each { |x, y| puts x.to_s + y.to_s }
> >
> > 12
> > 23
> > 34
> > 45
> >
> > This code doesn't work off course.
> > I can iterate using indices
> >
> > 0.upto(list.size-1) { |i| puts list[i] + list[i+1] }
> >
> > But it looks ugly to me. Do you know any elegant tricks that don't use
> > list indices?
> >
> > Thanks
> > FireAphis

>
> Enumerable#each_slice(n) {|...| ...}
>


Sorry Enumerable#each_cons(n) { }

you might need to require 'enumerable' though.

 
Reply With Quote
 
dblack@rubypal.com
Guest
Posts: n/a
 
      08-08-2007
Hi --

On Wed, 8 Aug 2007, Jano Svitok wrote:

> On 8/8/07, FireAphis <(E-Mail Removed)> wrote:
>> Hello,
>>
>> I need to iterate through a list and handle two elements on every
>> iteration. That is I'd like to do something like
>>
>> [1,2,3,4,5].each { |x, y| puts x.to_s + y.to_s }
>>
>> 12
>> 23
>> 34
>> 45
>>
>> This code doesn't work off course.
>> I can iterate using indices
>>
>> 0.upto(list.size-1) { |i| puts list[i] + list[i+1] }
>>
>> But it looks ugly to me. Do you know any elegant tricks that don't use
>> list indices?
>>
>> Thanks
>> FireAphis

>
> Enumerable#each_slice(n) {|...| ...}


That won't work because it won't double back; you'll get 12, 34, 5
instead of 12, 23, 34, 45.

However, you reminded me of something:

require 'enumerator'
[1,2,3,4,5].enum_cons(2).each {|a,b| puts "#{a}#{b}" }


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      08-08-2007
require 'labrador/enum'
a.zip(a.map.succ).map.join.first(-1)

but the version with first is not released yet

Robert

--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

 
Reply With Quote
 
dblack@rubypal.com
Guest
Posts: n/a
 
      08-08-2007
Hi --

On Wed, 8 Aug 2007, Robert Dober wrote:

> require 'labrador/enum'
> a.zip(a.map.succ).map.join.first(-1)
>
> but the version with first is not released yet


I can't quite follow how that will get to the result. Can you walk me
through it? My first reaction is that it's awfully full of "magic
dots", but I'm willing to be enlightened.... (And I honestly
can't puzzle it out.)


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      08-08-2007
On 8/8/07, (E-Mail Removed) <(E-Mail Removed)> wrote:
> Hi --
>
> On Wed, 8 Aug 2007, Robert Dober wrote:
>
> > require 'labrador/enum'
> > a.zip(a.map.succ).map.join.first(-1)

map without a parameter creates a Proxy object that contains the
enumeration and the method name :map.
It's method_missing forwards everything to the enumeration via send
and the method name, thus
map.join becomes
map{|x| x.send(:join)}

it is not everybody's cup of tea, but I love it, obviously.
Labrador, the LAZY programmers best friend

class EmptyProxy < EmptyObject
def initialize object, message
@enum = object
@message = message
end
end

class Dispatcher < EmptyProxy
def method_missing mth, *args, &blk
@enum.send(@message){|x| x.send(mth.to_sym,*args)}
end # def method_missing mth, *args, &blk
end # class Dispatcher < EmptyProxy

Cheers
Robert


--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

 
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
iterating over a list as if it were a circular list Sven Python 3 03-08-2013 05:59 PM
iterating over a list as if it were a circular list Sven Python 1 03-07-2013 01:37 PM
Iterating a std::vector vs iterating a std::map? carl C++ 5 11-25-2009 09:55 AM
OK to delete hash pairs while iterating through it? Jerry Krinock Perl Misc 7 09-12-2008 08:00 PM
Iterating over an area in pairs. Thaddeus L Olczyk Ruby 7 05-09-2006 09:44 AM



Advertisments