On PEP 312: simple implicit lambda
Stuart D. Gathman
stuart at bmsi.com
Tue Feb 18 22:36:00 EST 2003
On Sun, 16 Feb 2003 09:47:11 -0500, Christos TZOTZIOY Georgiou wrote:
> What I would do in such a case, would be the following:
>
> <read_more_data>:
> data = file_object.read(4096)
> while data:
> # process here
> <read_more_data>
>
> The syntax I imagined is the one proposed above, with angle brackets and
> all. What happens is this:
I did a much cleaner syntax of this idea for an assembler preprocessor.
Here is what it would look like in Python:
while when:
data = file_object.read(4096)
then data:
# process here
The idea is that WHEN introduces arbitrary code which "sets up" for
testing a condition, and THEN gets back to testing the condition. When
can also be used with 'if', and is useful when combined with short circuit
evaluation. Here is an example of the assembler code (IBM Series/1). The
WHEN cause lets me issue multiple statements to setup for the CFNEN (like
Intel "string" insns). The DO R6 provides an upper bound on the number of
loop iterations (an error checking feature nearly as useful as bounds
checking).
DO R6,WHILE,WHEN,REG=R6
MVWS BTCBLBUF,R5 COULD PSD AND USE R1 FOR THIS
MVW R4,R2
MVW R3,R7
CFNEN (R5),(R2)
THEN LLT
AW DLEN,R4 POINT R4 TO NEXT RECORD
ENDDO
One final asm example to show how WHEN is used with short circuiting
AND/OR:
IF (BTCBFLAG,OFF,BTCB at MWA+BTCB@MOV,TWI)
CALL BTBUF,(BTCBNODE,0)
ELSEIF (BTCBFLAG,ON,BTCB at MOV,TWI),OR,WHEN
CALL DISPOSE,TYPE=X
MVA BTCBNODE,R4 TRY TO AVOID RETRACE;
CALL GETBLK,TYPE=X GET BLOCK LAST SEEN IN
MVWS R3,BTCBABUF AND CHECK:
MVW R3,#NODE
THEN (BTROOT,NE,BTCBROOT,CD),OR SAME FILE
THEN (R4,P,BTSON,MVD),OR,WHEN LEAF NODE
IF (R4,Z,BTCOUNT,MVWS),THEN,POSTEOF NON EMPTY
THEN (R7,NP,BTCBSLOT,MVWS),OR,(R7,GT,R4),OR,WHEN VALID POS
CALL CHKKEY,TYPE=J MATCHING KEY
THEN NE,THEN
CALL BTTRACE,(BTCBKLEN) ELSE, RETRACE
ENDIF
The (op1,cond,op2,opcode) construct was short for
WHEN opcode op2,op1
THEN cond
In python syntax, that would be:
if (btcb.flag & (BTCB.MWA | BTCB.MOV)) != 0:
btbuf(btcb.node,None)
elif (btcb.flag & BTCB.MOV) != 0 or when:
dispose()
btcb.abuf = node = getblk(btcb.node)
then btroot != btcb.root or btson > 0 or when:
r4 = btcount
if r4 == 0: raise EOFException
r7 = btcb.slot
then r7 <= 0 or r7 > r4 or when:
rc = chkkey() # OK, this is artificial to mirror the ASM code
then rc:
bttrace(btcb.klen)
Flame away! I've always liked it because it allows any sequence of statements,
including nested loops and conditionals, to setup conditions in loops and
short-circuit boolean expressions.
--
Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.
More information about the Python-list
mailing list