in reply to An iterator for (not "iterating") a recursive data structure.

There is the simple "flatten as you go" approach:

sub genIterator { my @stack = @_; return sub { splice @stack, 0, 1, @{$stack[0]} while 'ARRAY' eq ref $stack[0]; shift @stack; } }

Replies are listed 'Best First'.
Re^2: An iterator for (not "iterating") a recursive data structure.
by BrowserUk (Patriarch) on Feb 14, 2015 at 23:02 UTC

    S'fine for small AoAoA..s, but the top level array in my application will contain upto 200 million elements -- circa. 7GB -- duplicating that on a stack would blow my memory.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      > duplicating that on a stack would blow my memory.

      It's not duplicating the memory, just caching the current path from root to leave, very similar to my solution here

      Worst case scenario with a balanced two level "tree" would mean approx @stack == 2*sqrt(@nodes) .

      For 200 million elements that's about 28000 elements.

      If that's still to many store a lazy iterator in the @stack returning the children of one node (like a function ref or an object or a tied array) and process it in the iterator.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice