[Python-ideas] Allowing breaks in generator expressions by overloading the while keyword

MRAB python at mrabarnett.plus.com
Fri Feb 21 23:54:05 CET 2014


On 2014-02-21 12:18, Nick Coghlan wrote:
> On 21 February 2014 20:24, Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> but alas both Nick Coglan and (if I recall correctly) Guido have ruled
>> that Python won't get this, so until the Revolution comes, it isn't
>> going to happen.
>
> It's not that it can't happen - it's that someone would need to build
> a case of a similar calibre to the one Chris Angelico is currently
> putting together for except expressions in PEP 463 :)
>
> However, it's a *much* bigger challenge in this case, as
> itertools.takewhile exists, whereas there's currently no way to do
> exception handling as part of a larger expression without major
> contortions.
>
> Re-reading Andrew's post at
> http://stupidpythonideas.blogspot.com.au/2013/07/syntactic-takewhile.html,
> I'm actually more intrigued by Haskell's shorthand for lambda
> functions that consist of a single binary operator.
>
> Consider:
>
>      isprime = all(n % p for p in takewhile((lambda p: p**2 < n), primes_seen))
>
> Is there are a nicer way to write that? The Haskell equivalent is:
>
>     (< n) . (** 2)
>
> That's not very readable to most Python programmers, but what if you
> could write something like:
>
>      isprime = all(n % p for p in takewhile((: ? ** 2 < n), primes_seen))
>
[snip]

C# has a lambda operator "=>". That would give:

     isprime = all(n % p for p in takewhile((x => x ** 2 < n), primes_seen))



More information about the Python-ideas mailing list