How do you implement a recursive walker of a tree with a lazy range?

Chris Cain clcain at uncg.edu
Wed Oct 30 09:50:32 PDT 2013


On Wednesday, 30 October 2013 at 14:58:21 UTC, Andrej Mitrovic 
wrote:
> It allocates, I'm looking for a lazy range. I would be 
> surprised that
> such a common task as iterating a tree is not possible without 
> using
> classes and workarounds.

It allocates, but it's still a lazy range. It only allocates when 
popFront is called. I looked around a bit more and found "only" 
in std.range that can do this without allocating:

With the walk function like this:
---
     InputRange!Tree walk()
     {
         return inputRangeObject(chain(
             only(this),
             children.map!(a=>a.walk).joiner));
     }
---

you would avoid allocations.


More information about the Digitalmars-d-learn mailing list