On Monday, 30 January 2017 at 22:34:11 UTC, TheFlyingFiddle wrote:
On Monday, 30 January 2017 at 11:03:52 UTC, Profile Anaysis
wrote:
I need to yield from a complex recursive function too allow
visualizing what it is doing.
e.g., if it is a tree searching algorithm, I'd like to yield
for each node so that the current state can be shown visually.
I realize that there are several ways to do this but D a yield
version without additional threads would be optimal. I don't
need concurrency or speed, just simple.
If you don't want to use fibers then an alternative to yeilds
is to use callbacks during iteration.
Example:
struct Tree(T)
{
T value;
Tree!(T)[] children;
}
void iterDepth(T)(Tree!(T) tree, void delegate(Tree!T) cb)
{
cb(tree);
foreach(child; tree.children)
{
iterDepth(child, cb);
}
}
unittest
{
auto tree = ... //Make the tree somehow
iterDepth(tree, (node)
{
writeln(node.value);
});
}
Callbacks have their set of problems but it's one of the
simpler ways to do this.
This can't be easily because it requires the callback to contain
the main program(if it depends on the results). Since I am
already in a multi-threaded environment it would not be easy to
marshal the data around.
If I run it in it's own thread then it won't block but seems like
a lot of work for a simple thing.