Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is this PEP-able? fwhile

Reply
Thread Tools

Is this PEP-able? fwhile

 
 
jimjhb@aol.com
Guest
Posts: n/a
 
      06-24-2013

Syntax:


fwhile X in ListY and conditionZ:


The following would actually exactly as: for X in ListY:


fwhile X in ListY and True:


fwhile would act much like 'for', but would stop if the condition after the'and' is no longer True.


The motivation is to be able to make use of all the great aspects of the python 'for' (no indexing or explict
end condition check, etc.) and at the same time avoiding a 'break' from the'for'.


(NOTE: Many people are being taught to avoid 'break' and 'continue' at allcosts, so they instead convert
the clean 'for' into a less-clean 'while'. Or they just let the 'for' run out. You can argue against this teaching
(at least for Python) but that doesn't mean it's not prevalent and prevailing.)


[People who avoid the 'break' by functionalizing an inner portion of the loop are just kidding themselves and making
their own code worse, IMO.]


I'm not super familiar with CPython, but I'm pretty sure I could get this up and working without too much effort.
The mandatory 'and' makes sense because 'or' would hold the end value valid(weird) and not accomplish much.
The condition itself could of course have multiple parts to it, including 'or's.


It's possible the name 'fwhile' is not optimal, but that shouldn't affect the overall merit/non-merit of the concept.


Comments and Questions welcome.


Thanks.

 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      06-26-2013
On Tue, 25 Jun 2013 17:20:43 +1000, Neil Hodgson <(E-Mail Removed)>
declaimed the following:

>(E-Mail Removed):
>
>> Syntax:
>> fwhile X in ListY and conditionZ:

>
> There is precedent in Algol 68:
>
>for i from 0 to n while safe(i) do .. od
>

The REXX variant would be

do for i = 0 to n while safe(i)
...
end

Basically one has an optional "for" clause ( for index = initial to end
by step ), and one has an optional while/until clause -- Hmm, wonder if
some interpreters would parse both while and until <G>. I need to install
Regina Rexx on this new machine...
--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      06-26-2013
On Wed, Jun 26, 2013 at 10:47 AM, Dennis Lee Bieber
<(E-Mail Removed)> wrote:
> On Tue, 25 Jun 2013 17:20:43 +1000, Neil Hodgson <(E-Mail Removed)>
> declaimed the following:
>
>>(E-Mail Removed):
>>
>>> Syntax:
>>> fwhile X in ListY and conditionZ:

>>
>> There is precedent in Algol 68:
>>
>>for i from 0 to n while safe(i) do .. od
>>

> The REXX variant would be
>
> do for i = 0 to n while safe(i)
> ...
> end
>
> Basically one has an optional "for" clause ( for index = initial to end
> by step ), and one has an optional while/until clause -- Hmm, wonder if
> some interpreters would parse both while and until <G>. I need to install
> Regina Rexx on this new machine...


Modulo the 'for' keyword, which is superfluous there. Here's a test
script I knocked up on my OS/2 box back home:

/* */
do i=0 to 9 while safe(i)
say i" is safe"
end
exit

safe: procedure
return arg(1)\=6

The \= in the last line is the REXX "not-equal" operator, like != in
Python. This outputs:

0 is safe
1 is safe
2 is safe
3 is safe
4 is safe
5 is safe

and then terminates. It's pretty clean; the DO/END construct defines a
block, and may optionally execute it more than once. With no
arguments, it just creates a block that executes once (equivalent to
C's braces); valid args include FOREVER (infinitely loop), WHILE
condition (iterate while condition is true), UNTIL condition (execute
once, then check condition, iterate while condition is false - like a
do/while in C), var=value (eg "I=1" - set var to value, then increment
by 1 or by the "BY" value, continue forever or until the "TO" value),
and possibly another that's slipped my mind. Aside from FOREVER, which
stands alone, they're completely independent.

But that's syntax, lots of it. What I'd like to see in Python is
simply a bit of flexibility in the rule about newlines. The for/if
construct in Python could be exactly the same as it now is, only with
the two statements on one line, and it would look very similar to the
existing notation. I can already one-line a simple statement:

for i in range(10): print(i)

I just can't put in an if:

>>> for i in range(10): if i%3: print(i)

SyntaxError: invalid syntax

But I can, as long as I use expression-if:

>>> for i in range(10): print(i) if i%3 else None


Seriously, I can use Perl-style notation to achieve this. Does that
seem right to you? *boggle*

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Who can explain this bug? mathog C Programming 57 06-11-2013 10:09 PM
How do I encode and decode this data to write to a file? cl@isbd.net Python 11 05-01-2013 11:36 PM
Why does this incorrect CRTP static_cast compile? kfrank29.c@gmail.com C++ 2 04-25-2013 01:38 PM
This looks like a Perl bug George Mpouras Perl Misc 18 04-21-2013 11:56 PM
Really throwing this out there - does anyone have a copy of my oldDancer web browser? steven.miale@gmail.com Python 1 04-10-2013 03:32 PM



Advertisments