Re: Non-list ranges

Kevan Heydon (kevan@Harston.CV.COM)
Tue, 26 Oct 93 14:03:51 GMT

Hello,

I have been follwing the thread on Non-list ranges with some interest and would
like to mention, as a little diversion, how our in house language (Bacis2) does it.
Bacis has the concept of iterator functions that "yield" values to a loop.
Hence you define an iterator function such:

my_range :- iter(min,max,step)
value :- min
loop
if value > max then
break
endif
yield value
value :- value + step
endloop
enditer

with a calling loop such as:

loop for var over my_range(1,20,2)
sys_show(var)
endloop

gives:
1
3
5
.
.
19

The statement "yield value" suspends the execution of the iterator and passes
"value" to the loop that called it. The next time round the calling loop when the
iterator is called again the suspended execution resumes after the yield statement.
Obviously the calling loop terminates when the iter function terminates.

While this type of functionality can be achieved by writing a function to return
a list, an iterator function does it without allocating space for a, potentialy
very long, list.

Could something like this be done in Python?

Kevan Heydon

Computervision R&D Ltd, Harston, England