develooper Front page | perl.perl6.language | Postings from May 2001

Re: Apoc2 - <STDIN> concerns

From:
Damian Conway
Date:
May 8, 2001 17:35
Subject:
Re: Apoc2 - <STDIN> concerns
Message ID:
200105090035.KAA66047@indy05.csse.monash.edu.au
 
   > 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



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About