Here's my current musings on "new python syntax features that would be
really, really, cool".
A common idiom[1] when looping is to do something like:
for i in somelist:
if not condition:
break
do_stuff()
Wouldn't it be cool[2] if instead you could do something like:
for i in somelist while condition:
do_stuff()
My use case for this was doing stuff with prime numbers where you'd
want to take the first however many from a big list of primes, e.g[3].
def is_prime(n, primes):
""" primes is a list of primes where sqrt(n) <= max(primes) < n"""
for p in primes while p*p <= n:
if n % p == 0:
return False
return True
Does this look like a sane piece of syntax to other people? If it
existed would you use it? Does something like this exist somewhere and
I'm just really slow?
Cheers,
Tim
[1] At least, I'm pretty sure i've done this before
[2] Cool is relative, I'm not seriously suggesting this should become
part of the language
[3] This is a rather contrived examples, but you get the idea.
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders