Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Synchronous but different iterators (http://www.velocityreviews.com/forums/t820742-synchronous-but-different-iterators.html)

Belorion 04-01-2005 11:08 PM

Synchronous but different iterators
 
(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.



gabriele renzi 04-01-2005 11:16 PM

Re: Synchronous but different iterators
 
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 }

Robert Klemme 04-02-2005 07:38 AM

Re: Synchronous but different iterators
 

"Belorion" <belorion@gmail.com> schrieb im Newsbeitrag
news:a48d774d050401150863a424fe@mail.gmail.com...
> (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


Belorion 04-02-2005 07:46 PM

Re: Synchronous but different iterators
 
Much appreciated! Thanks!

Matt




All times are GMT. The time now is 04:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.