This one time, at band camp, Tim Leslie wrote:
>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].

List comprehensions!

>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?

[p for p in [p in primes if p*p < n] if n % p == 0]

and then fold that all together ;-)
_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to