Quote:
Originally Posted by kovidgoyal
From a usability perspective I like range, however, range has th eissue of performance, which is why though the initial python implementation of range returned a list of numbers, in later versions it was changed to return an iterator over numbers.
|
I am worried about that. I am considering making range() a pseudo-function valid only in the context of a for statement. Because I know the context I can use the built-in range(), not generating the actual list. Something like this:
Code:
program:
for i in range(1, 10, 1) limit 1000:
<<something>>
rof
would be implemented as
Code:
generator = (str(j) for i,j in enumerate(range(start, stop, step)) if i < limit)
for lv in generator:
user_vars[loop_var] = lv
<<execute the block>>
The str() is needed because all values in the template language are strings.
It might be better from an error reporting standpoint not to check the limit in the generator but instead use an enumerate() when looping, checking the limit explicitly, and raising an exception if it is exceeded. Something like:
Code:
generator = (str(j) for j in range(start, stop, step))
for count, lv in enumerate(generator):
if count > limit:
raise (ERROR)
user_vars[loop_var] = lv
<<execute the block>>