Skip to content

Iterator

A cursor that walks a sequence once, front to back.

A class implements it with two methods: one steps to the next element and says whether there was one, the other reads the element that step landed on. You fix the element type where you declare the class, as in Iterator<string>. foreach calls both methods for you, and so does Core\Arr::from, so a cursor of your own stands anywhere a sequence is expected.

Walking a cursor uses it up. There is no rewind and no second pass, so a second foreach over the same one finds nothing left. For a collection somebody should be able to walk again, implement Iterable: that hands out a fresh cursor every time.

Good to know: a cursor has no keys, so a foreach over one binds a single variable. Reading the element before the first step, or after the sequence has ended, throws a LogicError.

Examples

One pass over an uploaded list

<?nvs
// A cursor of your own: this one walks the lines somebody uploaded and hands
// back only the ones that look like an email address. A `foreach` drives it, so
// the loop reads the same as a loop over an array.
class Addresses implements Iterator<string>
{
private array<string> $lines;
private int $at;
private int $count;
private string $address;
// How many lines were not addresses. The loop can read this when it is done.
public int $skipped;
public function constructor(string $upload)
{
$this->lines = Core\Str::split($upload, "\n");
$this->count = Core\Arr::count($this->lines) as int;
$this->at = -1;
$this->address = '';
$this->skipped = 0;
}
// One step forward. Blank lines and anything without an `@` are counted and
// stepped over here, so the loop never sees them. `false` means the end.
public function advance(): bool
{
$this->at = $this->at + 1;
while ($this->at < $this->count) {
string $line = Core\Str::trim($this->lines[$this->at]);
if (Core\Str::contains($line, '@')) {
$this->address = Core\Str::lower($line);
return true;
}
$this->skipped = $this->skipped + 1;
$this->at = $this->at + 1;
}
return false;
}
// The address the last step landed on.
public function current(): string
{
return $this->address;
}
}
string $upload = "Ada@Example.com\n\nnot an address\nbob@example.com\n Carol@Example.com \n";
var $addresses = new Addresses($upload);
foreach ($addresses as string $address) {
echo 'invite: ', $address, "\n";
}
echo $addresses->skipped, " lines were not addresses\n";
// A cursor is walked once, and this one is used up now. A second loop over the
// same cursor gets nothing, so keep the values if you need them twice.
foreach ($addresses as string $address) {
echo 'again: ', $address, "\n";
}
echo "the second loop printed nothing\n";
Output
invite: ada@example.com
invite: bob@example.com
invite: carol@example.com
3 lines were not addresses
the second loop printed nothing