develooper Front page | perl.perl6.language | Postings from December 2008

Re: Support for ensuring invariants from one loop iteration to the next?

Thread Previous | Thread Next
From:
David Green
Date:
December 16, 2008 09:28
Subject:
Re: Support for ensuring invariants from one loop iteration to the next?
Message ID:
09D21063-BE22-47BA-A225-0DCD3F0F86E7@telus.net
On 2008-Dec-6, at 7:37 am, Aristotle Pagaltzis wrote:
> Funnily enough, I think you’re onto something here that you didn’t  
> even notice: [...]
> if we had a NOTFIRST (which would run before ENTER just as FIRST  
> does, but on *every* iteration *except* the first), then we could  
> trivially attain the correct semantics and achieve all desired  
> results:
>
>    repeat {
>        @stuff = grep { !.valid }, @stuff };
>        NOTFIRST {
>            .do_something( ++$i ) for @stuff;
>        }
>    } while @stuff;
>
> The really nice thing about this is that the blocks are nested, so  
> that any variable in scope for the invariant enforcement will also  
> be in scope in the NOTFIRST block without the user ever having to  
> arrange another enclosing scope.

Oh, yes!  So what if we had "LOOP $n {}" that executed on the nth  
iteration?  "LOOP 0 {}" at the beginning would be like FIRST {}, "LOOP  
* {}" at the end would be like LAST {}, and "LOOP 1..* {}" would give  
us the NOTFIRST block.  Presumably you could have multiple LOOP blocks  
too.

(Actually, you couldn't quite do FIRST/LAST using LOOP $n, because  
FIRST/LAST occur outside ENTER/LEAVE, whereas LOOP would occur  
inside.  But perhaps you could have LOOP blocks inside ENTER/LEAVE  
blocks?)


-David


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About