Re: [CDBI] Re: CDBI::Iterator->first moves it's index pointer

[prev] [thread] [next] [Date index for 2004/11/10]

From: Dave Cash
Subject: Re: [CDBI] Re: CDBI::Iterator->first moves it's index pointer
Date: 21:36 on 10 Nov 2004
On Wed, 10 Nov 2004, Tony Bowden wrote:

> On Wed, Nov 10, 2004 at 06:23:15PM +0100, Rhesa Rozendaal wrote:
> > It turns out that "first" does this:
> > 	my $self = shift;
> > 	$self->reset;
> > 	return $self->next;
> > In other words, it moves the iterator to the next element, returning the
> > first.
>
> Not quite. It moves the iterator to before the start of the list, and
> then returns the 'next' one (which is, by definition, the first one.
>
> > I expected it to return the first element, and not move its index
> > pointer.
>
> That would be weird.
>
> Consider:
>
> 	my $it = run_some_search();
>
>   print $it->first;
>   print $it->next;
>   print $it->first;
>   print $it->next;
>
> Would you really expect this to print 1, 2, 1, 3? I'd say most people
> would expect 1, 2, 1, 2.
>
> I'm open to opinions here.

I too found this strange, as I expected first() to return the first
item rather than starting the iterator back at the first item for
subsequent next() calls.  To me, reset() is what I would expect to
reset the internal pointer.  So to get the 1, 2, 1, 2 output, you'd
do

  print $it->first;
  print $it->next;
  $it->reset;
  print $it->first;
  print $it->next;

Since there's no way to do something like $it->[0] without breaking
encapsulation, I guess I was hoping first() would do this.

I became familiar with all this when I was writing my patch
(re-submitted today since it didn't go through when I submitted it
the other day) to add previous() and last() methods to the Iterator
class.  I basically modelled last() after first(), in that it resets
the pointer to the end of the list and returns the last item.
previous() was harder because of the way next() works (it returns
the current item (at $it->{_place}) and then increments _place.
This is great if there's no such thing as previous().  So I reworked
the algorithm a bit to make next() increment first and then return
the item.  And previous() decrements first and then returns the
item.  I had to do a few tricks to get next() to keep working the
way it already did (i.e. for all the iterator tests to pass), but in
the end all was well.

Sorry if this is all a bit off-thread.  I just thought it might be
relevant since this thread is about Iterator algorithms.

BTW, I just had a thought: how about making the Iterator class a
package for dealing with a list instead of a package for dealing
with a hash?  Would there be a downside to this?

Dave

/L\_/E\_/A\_/R\_/N\_/T\_/E\_/A\_/C\_/H\_/L\_/E\_/A\_/R\_/N\
Dave Cash                              Power to the People!
Frolicking in Fields of Garlic               Right On-Line!
dave@xxxxx.xxx                                  Dig it all.

CDBI::Iterator->first moves it's index pointer
Rhesa Rozendaal 17:23 on 10 Nov 2004

Re: CDBI::Iterator->first moves it's index pointer
Rhesa Rozendaal 21:31 on 10 Nov 2004

Re: [CDBI] Re: CDBI::Iterator->first moves it's index pointer
Dave Cash 21:36 on 10 Nov 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52