Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: recognizing empty iterators

Reply
Thread Tools

Re: recognizing empty iterators

 
 
Bengt Richter
Guest
Posts: n/a
 
      07-22-2003
On 21 Jul 2003 07:26:15 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Michele Simionato) wrote:

>After a recent thread on .endswith, I have been thinking about iterators.
>I realized that I don't know a satisfactory way to check if an
>iterator is empty. In other words I am looking for an
>"isempty" function to use in "if" statements such as
>
>if isempty(iterator):
> do_something()
>
>without side effects. Here are some unsatisfactory ways of implementing
>"isempty":
>
>#1: converting to a list or tuple
>def isempty(iterator):
> return not list(iterator)
>
>Easy, but one has to create the entire list, thus defecting the basic
>purpose of the iterator, i.e. lazy evaluation.
>
>#2: checking for StopIteration
>def isempty(iterator):
> try:
> iterator.next()
> except StopIteration:
> return True
> else:
> return False
>
>This works, for instance
>
>print isempty(iter([]))
>
>gives True and
>
>it=iter([1,2,3])
>print isempty(it)
>
>gives False. However, there is a side effect: after the check, the
>iterator has advanced of one step and now "it.next()" gives 2, not 1.
>In order this to work without side effects, I should be able to restart
>the iterator from the beginning, but I don't know how to do that.
>Is it possible?
>
>#3: comparing with the empty iterator
>
>emptyiterator=iter([])
>
>it=iter([])
>
>if it == emptyiterator: print 'Ok!'
>
>This simply doesn't work.
>
>I wonder if the itertools module should contain a function to check for
>empty iterators, thus simplifying my life Of course, I may well be
>missing something obvious, if so, please enlighten me.
>

If iterators had a .peek() method for 1-item lookahead, maybe that would
be easy to implement. If empty, it could raise StopIteration, and otherwise
return what next() would return, without disturbing the state w.r.t. next().

Alternatively, it could return [nextitem] vs [] if at the end
(without raising StopIteration).

Regards,
Bengt Richter
 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      07-22-2003

"Bengt Richter" <(E-Mail Removed)> wrote in message
news:bfhvnk$hvd$0@216.39.172.122...
> On 21 Jul 2003 07:26:15 -0700, (E-Mail Removed) (Michele Simionato) wrote:
>
> >After a recent thread on .endswith, I have been thinking about iterators.
> >I realized that I don't know a satisfactory way to check if an
> >iterator is empty. In other words I am looking for an
> >"isempty" function to use in "if" statements such as
> >
> >if isempty(iterator):
> > do_something()
> >
> >without side effects. Here are some unsatisfactory ways of implementing
> >"isempty":
> >
> >#1: converting to a list or tuple
> >def isempty(iterator):
> > return not list(iterator)
> >
> >Easy, but one has to create the entire list, thus defecting the basic
> >purpose of the iterator, i.e. lazy evaluation.
> >
> >#2: checking for StopIteration
> >def isempty(iterator):
> > try:
> > iterator.next()
> > except StopIteration:
> > return True
> > else:
> > return False
> >
> >This works, for instance
> >
> >print isempty(iter([]))
> >
> >gives True and
> >
> >it=iter([1,2,3])
> >print isempty(it)
> >
> >gives False. However, there is a side effect: after the check, the
> >iterator has advanced of one step and now "it.next()" gives 2, not 1.
> >In order this to work without side effects, I should be able to restart
> >the iterator from the beginning, but I don't know how to do that.
> >Is it possible?
> >
> >#3: comparing with the empty iterator
> >
> >emptyiterator=iter([])
> >
> >it=iter([])
> >
> >if it == emptyiterator: print 'Ok!'
> >
> >This simply doesn't work.
> >
> >I wonder if the itertools module should contain a function to check for
> >empty iterators, thus simplifying my life Of course, I may well be
> >missing something obvious, if so, please enlighten me.
> >

> If iterators had a .peek() method for 1-item lookahead, maybe that would
> be easy to implement. If empty, it could raise StopIteration, and

otherwise
> return what next() would return, without disturbing the state w.r.t.

next().
>
> Alternatively, it could return [nextitem] vs [] if at the end
> (without raising StopIteration).


I think that's two separate functions. isEmpty() is not the same
thing as willBeEmptyOnNextCall().

That said, there are places where a 1 item lookahead can be quite
valuable, and lack of a sliding window is the last major thing I'd
like to see addressed in the for loop.

John Roth

>
> Regards,
> Bengt Richter



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM
Testing for empty iterators? Roy Smith Python 7 07-06-2004 04:29 PM
Recognizing / Not Recognizing the Disk Drives Bill Coleman Computer Support 1 05-31-2004 07:36 AM
Re: recognizing empty iterators Steven Taschuk Python 5 07-23-2003 12:21 PM



Advertisments