0

I'm about to implement pythons itertools.groupby in PHP 5.4; because I need it.

It is an iterator that takes an iterator as input and returns a sequence of iterators, exactly what it does it unimportant for this question.

It seems like this should be a RecursiveIterator; but I cant figure what the purpose of that interface is.

Why would you not just return the iterators directly instead of taking the detour over hasChildren ? And the other thing; what should the value then be?

It is possible that I'm just wrong; GroupBy should not be a RecursiveIterator; but that's the question. What is the purpose/usecase for RecursiveIterator?

Edit: This question us not a duplicate of "How does RecursiveIteratorIterator work in PHP?" any more than a question about arrays would be completely answered by an in depth discussion about ArrayIterator.

This question is about RecursiveIterator, why you use it, when it is appropriate to use et cetera. I'm not interested in some concrete class that uses the interface.

Odalrick
  • 645
  • 5
  • 16

1 Answers1

2

An iterator iterates over a collection. Say, given this structure:

array(
    'foo',
    'bar',
    array(
        'baz'
    )
)

A regular Iterator would iterate over this structure and return foo, bar and Array. A RecursiveIterator has methods to signal hey, there's a child I could iterate over if you wanted me to (RecursiveIterator::hasChildren and RecursiveIterator::getChildren). You can write code to detect the presence of these children and get sub-iterators yourself, or you can use a RecursiveIteratorIterator, which does this for you and lets you iterate over the structure as if it was a flat list, returning foo, bar, baz.

foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as ...)

The distinction exists so you can:

  • iterate over a flat list, or
  • iterate over a flat list with "manual recursion", or
  • iterate over a recursive structure as if it was a flat list

I would say the RecursiveIterator interface is rarely used directly by "consumers", but it's a building block of RecursiveIteratorIterators and it allows you to easily define your own iterators for your own recursive data structures to be iterated over by a RecursiveIteratorIterator. It allows you to cheaply implement recursion by just defining two methods: one which checks if an element is iterable, and another which returns an iterator for it.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • So a RecursiveIterator iterates over a tree, where each node has both a value and possibly children. Closely related: OuterIterator has _both_ a value and children. This begs the question; is there an obvious difference between a RecusiveIterator and a hypothetical TreeIterator; or is it just a case of choosing one name over the other? – Odalrick Dec 20 '13 at 15:04
  • Not exactly. A `RecursiveIterator` iterates over a list with the option to get sub-iterators. A `RecursiveIterarorIterator` iterates over the recursive structure the `RecursiveIterator` provides the access to. A "TreeIterator" would presumably combine both those tasks into one class, instead of breaking it down into two. – deceze Dec 20 '13 at 15:37