Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Synchronous but different iterators

Reply
Thread Tools

Synchronous but different iterators

 
 
Belorion
Guest
Posts: n/a
 
      04-01-2005
(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.


 
Reply With Quote
 
 
 
 
gabriele renzi
Guest
Posts: n/a
 
      04-01-2005
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 }
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-02-2005

"Belorion" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> (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/libdo...doc/index.html

Kind regards

robert

 
Reply With Quote
 
Belorion
Guest
Posts: n/a
 
      04-02-2005
Much appreciated! Thanks!

Matt


 
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
Difference between synchronous (proactor pattern) and synchronous model in web server Rickert C++ 0 10-06-2011 04:54 AM
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Synchronous write to same file using different file pointers Alexander Dünisch Java 5 05-03-2008 03:33 AM
Bad synchronous description, but why ? Roman Himmes VHDL 8 06-22-2005 03:46 PM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM



Advertisments