Skip to content

KeyedIterator

The interface for a cursor that moves through entries one at a time, each with a key and a value.

A KeyedIterator<K, V> has three methods. advance moves to the next entry and returns true. If there is no next entry, it returns false. key returns the key of the entry that advance just moved to, and current returns its value. You write the types when you declare the class, for example implements KeyedIterator<int, string>. A class without type arguments does not compile.

A foreach loop can use a cursor directly: foreach ($cursor as var $key => var $value). The loop calls advance before each step and stops when it returns false. A cursor is used only once. A second loop over the same cursor visits nothing. If you need to walk the entries more than once, write a class that implements KeyedIterable and returns a new cursor each time.

Good to know: key and current throw a LogicError before the first advance and after advance returns false. A class that does not have all three methods does not compile, with the error E0449.

Examples

A repayment plan read once

<?nvs
// A loan repayment plan written as a cursor that a `foreach` loop drives directly.
// The key is the month number, and the value is what is still owed after that month.
class RepaymentPlan implements KeyedIterator<int, int>
{
private int $owed;
private int $payment;
private int $month = 0;
private bool $done = false;
public function constructor(int $loan, int $payment)
{
$this->owed = $loan;
$this->payment = $payment;
}
// Each call moves to the next month and takes off one payment. It returns `false` once nothing is owed.
public function advance(): bool
{
if ($this->owed <= 0) {
$this->done = true;
return false;
}
$this->month = $this->month + 1;
$this->owed = $this->owed - $this->payment;
if ($this->owed < 0) {
$this->owed = 0;
}
return true;
}
public function key(): int
{
$this->check();
return $this->month;
}
public function current(): int
{
$this->check();
return $this->owed;
}
// `key` and `current` throw a `LogicError` before the first month and after the last one.
private function check(): void
{
if ($this->month == 0 || $this->done) {
throw new LogicError('no current month');
}
}
}
var $plan = new RepaymentPlan(1000, 300);
foreach ($plan as int $month => int $owed) {
echo 'month ', $month, ': ', $owed, " still owed\n";
}
// The cursor is at its end now, so this loop prints nothing.
foreach ($plan as int $month => int $owed) {
echo 'again: month ', $month, "\n";
}
// Reading the key after the end throws a `LogicError`.
try {
echo $plan->key(), "\n";
} catch (LogicError $e) {
echo 'LogicError: ', $e->message, "\n";
}
Output
month 1: 700 still owed
month 2: 400 still owed
month 3: 100 still owed
month 4: 0 still owed
LogicError: no current month

Changelog

VersionChange
0.0.5foreach gives the key and the value of a Core\ObjectMap: write foreach ($map as var $key => var $value). A class of your own can do the same by implementing KeyedIterable<K, V>.