Synchronous but different iterators

B

Belorion

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

gabriele renzi

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 }
 
R

Robert Klemme

Belorion said:
(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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top