Synchronous but different iterators

Discussion in 'Ruby' started by Belorion, Apr 2, 2005.

  1. Belorion

    Belorion Guest

    (somewhat contrived example:) Say I have 2 different data structures
    which I want to iterate over, and Tree, and an Array. I had to write
    specific iterators for my Tree structure, such as depth_first,
    breadth_first, etc. Is there a good way to iterate over each
    structure in a synchronous manner, but with a different iteration
    style?

    For example, I want to step through my Array normally using
    each_index, but want to step through my Tree structure using, say,
    depth_first, but I want to do this concurrently because of what I want
    to do to/with the data.

    "pseudo" ruby code:
    (myTree.depth_first.each, myArray.each_index){ |tree_node, array_element|
    # do stuff with each in tandem
    }

    Is there a trick to doing this? Or is my best bet to iterate over my
    Tree object and create a temporary array, and then just iterate in
    step using with_index?

    tmpArr = []
    myTree.depth_first{ |node| tmpArr.push node }

    myArray.each_index{ |ii|
    tree_node, array_element = tmpArr[ii], myArray[ii]
    }

    The first approach would raise some questions such as what to do if
    one iterator reaches the end before the other ... and one would have
    to decide wether the "next" object in that iteration is nil, or if the
    synchronous iterator terminates whenever the shortest iteration
    reaches the end.

    Obviously, the (2nd) working ruby solution is not that difficult, but
    I am more curious from an academic standpoint if the first approach is
    possible through some fancy yield tricks.
     
    Belorion, Apr 2, 2005
    #1
    1. Advertising

  2. Belorion ha scritto:
    <snipall>
    not sure I understood right, but maybe:

    see the Enumerator module, it allows you to create an object wich
    proxies a method like #each_preorder into an #each one, so after that
    you can simply do:
    mytree.zip(mytree_enumerator){|x,y| do stuff }
     
    gabriele renzi, Apr 2, 2005
    #2
    1. Advertising

  3. "Belorion" <> schrieb im Newsbeitrag
    news:...
    > (somewhat contrived example:) Say I have 2 different data structures
    > which I want to iterate over, and Tree, and an Array. I had to write
    > specific iterators for my Tree structure, such as depth_first,
    > breadth_first, etc. Is there a good way to iterate over each
    > structure in a synchronous manner, but with a different iteration
    > style?
    >
    > For example, I want to step through my Array normally using
    > each_index, but want to step through my Tree structure using, say,
    > depth_first, but I want to do this concurrently because of what I want
    > to do to/with the data.
    >
    > "pseudo" ruby code:
    > (myTree.depth_first.each, myArray.each_index){ |tree_node, array_element|
    > # do stuff with each in tandem
    > }
    >
    > Is there a trick to doing this? Or is my best bet to iterate over my
    > Tree object and create a temporary array, and then just iterate in
    > step using with_index?
    >
    > tmpArr = []
    > myTree.depth_first{ |node| tmpArr.push node }
    >
    > myArray.each_index{ |ii|
    > tree_node, array_element = tmpArr[ii], myArray[ii]
    > }
    >
    > The first approach would raise some questions such as what to do if
    > one iterator reaches the end before the other ... and one would have
    > to decide wether the "next" object in that iteration is nil, or if the
    > synchronous iterator terminates whenever the shortest iteration
    > reaches the end.
    >
    > Obviously, the (2nd) working ruby solution is not that difficult, but
    > I am more curious from an academic standpoint if the first approach is
    > possible through some fancy yield tricks.


    You should look at Generator of the standard lib. That does exactly what
    you want:
    http://www.ruby-doc.org/stdlib/libdoc/generator/rdoc/index.html

    Kind regards

    robert
     
    Robert Klemme, Apr 2, 2005
    #3
  4. Belorion

    Belorion Guest

    Much appreciated! Thanks!

    Matt
     
    Belorion, Apr 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Himmes
    Replies:
    8
    Views:
    13,854
    Duane Clark
    Jun 22, 2005
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    519
    Kai-Uwe Bux
    May 8, 2005
  3. Alexander Dünisch
    Replies:
    5
    Views:
    938
    Alexander Dünisch
    May 3, 2008
  4. , India
    Replies:
    10
    Views:
    1,104
    James Kanze
    Aug 8, 2009
  5. Rickert
    Replies:
    0
    Views:
    718
    Rickert
    Oct 6, 2011
Loading...

Share This Page