On Thu, Aug 19, 2004 at 12:31:42PM -0700, Larry Wall wrote:
> So let's rewrite the table (assuming that all the hash methods are just
> variants of .values), where N and D are non-destructing and destructive:
>
> next D next N all D all N
> ====== ====== ===== =====
> $iter $iter.read ?1 $iter.read ?2
> @array @array.shift @array.for @array.splice @array
> $array $array.shift $array.for $array.splice @$array
> %hash ?3 %hash.values ?4 %hash.values
> $hash ?3 $hash.values ?4 $hash.values
>
> Hmm. Ignore ?1 and ?2, since it's not clear that iterators can be
> read non-destructively.
In scalar context a non-destructive read of an iterator might
be called $iter.peek and the next .read will get (and remove)
the same value that .peek returns. Implementation would be
fairly simple - the control info for an iterator would have a
field containing the next value and a flag to specify whether
that value had been determined yet.
sub peek {
unless ($iter.flag) {
$iter.nextval = $iter.getnext();
$iter.flag = true;
}
return $iter.nextval;
}
sub read {
if ($iter.flag) {
$iter.flag = false;
return $iter.nextval;
}
return $iter.getnext();
}
--