Suggesting a new feature - "Inverse Generators"

Tim Hochberg tim.hochberg at ieee.org
Fri Mar 25 11:25:30 EST 2005


Jordan Rastrick wrote:

[CHOP]

> Behold:
> 
> # An Acceptor/Generator!!!
> def combineIntoRecords():
>        optionalline = None # We may not get given a value for this line
>        accept firstline
>        accept secondline
>        if condition(secondline):
>           accept optionalline
>        accept lastline
>        yield createRecord(firstline, secondline, optionalline,
> lastline)

That's a nice example, but why not:

def combineIntoRecords(iterable):
     iterator = iter(iterable)
     optionalline = None
     firstline = iterator.next()
     secondline = iterator.next()
     if condition(secondline):
         optionalline = iterator.next()
     lastline = iterator.next()
     yield createRecord(firstline, secondline, optionalline, lastline)

???

-tim

> 
> def getData(lines):
> 	return list(combineIntoRecords(filterJunk(lines)))
> 
> So, alas, my beloved for loop was not saved after all.
> 
> Fortunately, the dreaded While loop has been vanquished. In fact all
> loopiness has disappeared. Along with any other semblance of a main
> method.
> 
> I think I like the finished result. I'll leave if for you to decide if
> you like it too.
> 




More information about the Python-list mailing list