[1, 2, 3, 4, 5] arr2 = [6,7,8,9,10] --> [6, 7, 8, 9, 10] def both( enum1, enum2 ) enumC = enum2.dup enum1.each do |e1| yield e1, enumC.shift end end --> nil both( arr1, arr2 ) do |a,b| puts "#{a} : #{b}" end 1 : 6 2 : 7 3 : 8 4 : 9 5 : 10 --> [1, 2, 3, 4, 5] It assumes that enum1 is at least at big as enum2. Is there a way someone can think of that's more efficient then this? Namely getting rid of the 'dup'. Or maybe I'm missing some rubyism that would make this easier. TIA, Michael [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <3F68ED0D.3080900@earthlink.net> [ref] => [htmlstate] => on_nl2br [postusername] => Michael Garriss [ip] => mgarriss@earthl [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 1 [islastshown] => [isfirstshown] => 1 [attachments] => [allattachments] => ) --> [1, 2, 3, 4, 5] > arr2 = [6,7,8,9,10] > --> [6, 7, 8, 9, 10] > def both( enum1, enum2 ) > enumC = enum2.dup > enum1.each do |e1| > yield e1, enumC.shift > end > end > --> nil > both( arr1, arr2 ) do |a,b| > puts "#{a} : #{b}" > end > 1 : 6 > 2 : 7 > 3 : 8 > 4 : 9 > 5 : 10 > --> [1, 2, 3, 4, 5] > > > It assumes that enum1 is at least at big as enum2. > > Is there a way someone can think of that's more efficient then this? > Namely getting rid of the 'dup'. Or maybe I'm missing some rubyism > that would make this easier. >[/quote] I just realized that a) my spell checker doesn't look at the subject line, and that b) this only works for arrays. How could I make it work (well) for any enumerable? Michael [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <3F68EDCA.3050909@earthlink.net> [ref] => <3F68ED0D.3080900@earthlink.net> [htmlstate] => on_nl2br [postusername] => Michael Garriss [ip] => mgarriss@earthl [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 2 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> going over two interators - Ruby
Professional Web Applications Themes

going over two interators - Ruby

I often find that I need to go over two enumerables in parallel. I have used a little method called 'both' for this: irb arr1 = [1,2,3,4,5] --> [1, 2, 3, 4, 5] arr2 = [6,7,8,9,10] --> [6, 7, 8, 9, 10] def both( enum1, enum2 ) enumC = enum2.dup enum1.each do |e1| yield e1, enumC.shift end end --> nil both( arr1, arr2 ) do |a,b| puts "#{a} : #{b}" end 1 : 6 2 : 7 3 : 8 4 : 9 5 : 10 --> [1, 2, 3, 4, 5] It assumes that enum1 is at least at ...

  1. #1

    Default going over two interators

    I often find that I need to go over two enumerables in parallel. I have
    used a little method called 'both' for this:

    irb
    arr1 = [1,2,3,4,5]
    --> [1, 2, 3, 4, 5]
    arr2 = [6,7,8,9,10]
    --> [6, 7, 8, 9, 10]
    def both( enum1, enum2 )
    enumC = enum2.dup
    enum1.each do |e1|
    yield e1, enumC.shift
    end
    end
    --> nil
    both( arr1, arr2 ) do |a,b|
    puts "#{a} : #{b}"
    end
    1 : 6
    2 : 7
    3 : 8
    4 : 9
    5 : 10
    --> [1, 2, 3, 4, 5]


    It assumes that enum1 is at least at big as enum2.

    Is there a way someone can think of that's more efficient then this?
    Namely getting rid of the 'dup'. Or maybe I'm missing some rubyism that
    would make this easier.

    TIA,
    Michael


    Michael Garriss Guest

  2. #2

    Default Re: going over two interators

    Michael Garriss wrote:
    > I often find that I need to go over two enumerables in parallel. I
    > have used a little method called 'both' for this:
    >
    > irb
    > arr1 = [1,2,3,4,5]
    > --> [1, 2, 3, 4, 5]
    > arr2 = [6,7,8,9,10]
    > --> [6, 7, 8, 9, 10]
    > def both( enum1, enum2 )
    > enumC = enum2.dup
    > enum1.each do |e1|
    > yield e1, enumC.shift
    > end
    > end
    > --> nil
    > both( arr1, arr2 ) do |a,b|
    > puts "#{a} : #{b}"
    > end
    > 1 : 6
    > 2 : 7
    > 3 : 8
    > 4 : 9
    > 5 : 10
    > --> [1, 2, 3, 4, 5]
    >
    >
    > It assumes that enum1 is at least at big as enum2.
    >
    > Is there a way someone can think of that's more efficient then this?
    > Namely getting rid of the 'dup'. Or maybe I'm missing some rubyism
    > that would make this easier.
    >
    I just realized that a) my spell checker doesn't look at the subject
    line, and that b) this only works for arrays. How could I make it work
    (well) for any enumerable?

    Michael


    Michael Garriss Guest

  3. #3

    Default Re: going over two interators

    On Thu, 18 Sep 2003 08:27:08 +0900, Michael Garriss wrote:

    In Ruby 1.8, try Enumerable#zip.

    -austin
    --
    austin ziegler * [email]austinhalostatue.ca[/email] * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.09.17
    * 21.58.04



    Austin Ziegler Guest

  4. #4

    Default Re: going over two interators

    Austin Ziegler wrote:
    >In Ruby 1.8, try Enumerable#zip.
    >
    >
    That's the one. Thanks to you and the lucky stiff....

    Michael


    Michael Garriss Guest

  5. #5

    Default Re: going over two interators

    On Thu, Sep 18, 2003 at 08:23:59AM +0900, Michael Garriss wrote:
    > I often find that I need to go over two enumerables in parallel.
    Have a look at these:

    [url]http://www.rubycentral.com/faq/rubyfaq-5.html#ss5.5[/url]
    [url]http://www.rubygarden.org/article.php?sid=107[/url]
    [url]http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/rubycollections/rubycollections/rbc/iterator/[/url]
    [url]http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/rough/lib/generator.rb?rev=1.6&content-type=text/x-cvsweb-markup[/url]
    [url]http://rgl.sourceforge.net/files/stream_rb.html[/url]

    Paul


    Paul Brannan Guest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139