[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Synchronous but different iterators

Belorion

4/1/2005 11:09:00 PM

(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.


5 Answers

gabriele renzi

4/1/2005 11:17:00 PM

0

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

4/2/2005 7:38:00 AM

0


"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/libdoc/generator/rdoc/...

Kind regards

robert

Belorion

4/2/2005 7:47:00 PM

0

Much appreciated! Thanks!

Matt


Kip Williams

6/30/2010 2:44:00 AM

0

O wrote:
> In article<mzuWn.12721$4B7.367@newsfe16.iad>, Kip Williams
> <kip@rochester.rr.com> wrote:
>
>> O wrote:
>>> In article<ECmWn.8281$YX3.871@newsfe18.iad>, Kip Williams
>>> <kip@rochester.rr.com> wrote:
>>>
>>>> Bob Harper wrote:
>>
>>>>> An excellent analogy. We can 'enjoy', if that is the right word, Matthew
>>>>> Brady's Civil War photos, but might we not get more out of them with
>>>>> better photo technology?
>>>>
>>>> I believe many of them are available in true stereo.
>>>
>>> And many of them were post-processed, battle scenes arranged after the
>>> fact to fit the camera. Not only true stereo, but fake, "enhanced"
>>> stereo too.
>>
>> I should have known there'd be two sides to it.
>
> It was a long playing war.

Because of the low rate of revolutions involved.


Kip W

William Sommerwerck

6/30/2010 12:42:00 PM

0

>>> And many of them were post-processed, battle scenes
>>> arranged after the fact to fit the camera. Not only true
>>> stereo, but fake, "enhanced" stereo, too.

>> How was this done in the 19th century?

> After the battles, Brady (and his many assistants) would arrange
> many of the bodies in the scene to get the effects they wanted.

But rearranging the bodies doesn't produce "fake" or "enhanced" stereo --
even if the bodies were positioned for the best stereo effect.

"Fake" stereo would be the alteration of a 2D image to produce a 3D effect.
That would have been very difficult to do.