> Doubtless Damian could come up with a way to view them as hashes...
Well, of course.
An iterator is neither pure state nor pure behaviour. It's an
inextricable commingling of the two. In other words: an object.
So they are *most naturally* viewed as hashes:
package Iterator;
sub new { my $class = shift; bless {@_}, $class }
sub next { croak 'Abstract next method called on ' . ref $_[0] }
sub reset { my $self = shift; $self->{next} = undef; }
package Iterator::Range;
use base 'Iterator';
sub next {
my $self = shift;
$self->{next} = $self->{from} unless defined $self->{next};
return $self->{next}++ if $self->{next} <= $self->{to};
$self->{next} = undef;
return;
}
package Iterator::Step;
use base 'Iterator::Range';
sub next {
my $self = shift;
$self->{next} = $self->{from} unless defined $self->{next};
return ($self->{next}+=$self->{step})-$self->{step}
if $self->{next} <= $self->{to};
$self->{next} = undef;
return;
}
package Iterator::Array;
use base 'Iterator';
sub next {
my $self = shift;
return ($self->{array}[$self->{next}++]
if ($self->{next}||=0) < @{$self->{array}};
$self->{next} = undef;
return;
}
# etc.
# etc.
# etc.
See? Nothing but hashes.
;-)
Damian