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

2008-12-19 Thread Aristotle Pagaltzis
* Jon Lang datawea...@gmail.com [2008-12-19 03:50]: Personally, it doesn’t strike me as being as straightforward as putting a “last unless” clause into the middle of an otherwise-infinite loop You have to keep more state in your head to read while(1) { # ... last if $foo;

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

2008-12-19 Thread Jon Lang
Like I said: if the goto approach works for you, more power to you. Me, I find: loop { @stuff = grep { $_-valid } @stuff; TEST: last unless @stuff; $_-do_something( ++$i ) for @stuff; } to be at least as straightforward as: goto INVARIANT; while ( @stuff ) {

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

2008-12-18 Thread David Green
On 2008-Dec-17, at 5:15 pm, Aristotle Pagaltzis wrote: The way Template Toolkit solves this is far better: the loop body gets access to an iterator object which can be queried for the count of iterations so far and whether this is the first or last iteration. Well, I prefer a built-in counter

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

2008-12-18 Thread Aristotle Pagaltzis
* David Green david.gr...@telus.net [2008-12-18 19:45]: Well, I prefer a built-in counter like that, but I thought the point was that you wanted some kind of block or something that could be syntactically distinct? No, that would only be a means to the end. That end is simply to not repeat

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

2008-12-18 Thread Larry Wall
On Fri, Dec 19, 2008 at 01:47:08AM +0100, Aristotle Pagaltzis wrote: : And I just realised how to best do that in Perl 5: : : goto INVARIANT; : : while ( @stuff ) { : $_-do_something( ++$i ) for @stuff; : : INVARIANT: : @stuff = grep { $_-valid } @stuff; : }

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

2008-12-18 Thread Jon Lang
Aristotle Pagaltzis wrote: And it says exactly what it's supposed to say in the absolutely most straightforward manner possible. The order of execution is crystal clear, the intent behind the loop completely explicit. If it works for you, great! Personally, it doesn't strike me as being as

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

2008-12-17 Thread Aristotle Pagaltzis
* David Green david.gr...@telus.net [2008-12-16 18:30]: So what if we had LOOP $n {} that executed on the nth iteration? Ugh. Please no. Now you suddenly have this odd new corner of the language with all its very own semantics and you have to figure out how to make it orthogonal enough and when

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

2008-12-16 Thread David Green
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

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

2008-12-16 Thread Timothy S. Nelson
On Tue, 16 Dec 2008, David Green wrote: 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

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

2008-12-16 Thread Jon Lang
How do you compute '*'? That is, how do you know how many more iterations you have to go before you're done? Should you really be handling this sort of thing through an iteration count mechanism? How do you keep track of which iteration you're on? Is it another detail that needs to be handled

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

2008-12-16 Thread Timothy S. Nelson
On Tue, 16 Dec 2008, Jon Lang wrote: How do you compute '*'? That is, how do you know how many more iterations you have to go before you're done? In some cases, it won't have to be computed, in some cases it won't be computeable (which should be an error). I'd say it'd be fair enough to

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

2008-12-16 Thread David Green
On 2008-Dec-16, at 6:21 pm, Timothy S. Nelson wrote: Or, instead of having a new block, just add the iterator indicator to the NEXT block, and get rid of ENTER and LEAVE. That way, you'd have this sequence: - FIRST {} - NEXT 0 {} # Replaces ENTER - NEXT 1..* {} # Does

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

2008-12-06 Thread John Macdonald
On Thu, Dec 04, 2008 at 04:40:32PM +0100, Aristotle Pagaltzis wrote: * [EMAIL PROTECTED] [EMAIL PROTECTED] [2008-12-03 21:45]: loop { doSomething(); next if someCondition(); doSomethingElse(); } I specifically said that I was aware of this solution and that I am dissatisfied

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

2008-12-06 Thread Aristotle Pagaltzis
* David Green [EMAIL PROTECTED] [2008-12-05 15:45]: I tried to break down the reasons for wanting to write such loops different ways: 1) Simple code […] 2) Clear code […] 3) Self-documenting code […] Yes, exactly right. What we need is something a bit like the continue block, except

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

2008-12-05 Thread David Green
On 2008-Dec-4, at 9:09 am, Aristotle Pagaltzis wrote: And while it does seems like a closure trait, that seems somewhat problematic in that the order of evaluation is weird when compared to other closure traits, which I suppose is what led you to declare the “coy” solution as the most

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

2008-12-05 Thread David Green
On 2008-Dec-5, at 7:43 am, David Green wrote: Now the condition is in the middle and is syntactically separate. (It's still not up front, but if the first block is really long, you can always... add a comment!) Well, you don't need a comment -- why not allow the condition to come first?

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

2008-12-04 Thread Aristotle Pagaltzis
* [EMAIL PROTECTED] [EMAIL PROTECTED] [2008-12-03 21:45]: loop { doSomething(); next if someCondition(); doSomethingElse(); } I specifically said that I was aware of this solution and that I am dissatisfied with it. Did you read my mail? * Jon Lang [EMAIL PROTECTED] [2008-12-03

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

2008-12-04 Thread Aristotle Pagaltzis
* Mark J. Reed [EMAIL PROTECTED] [2008-12-03 20:30]: OK, so let's look at the general problem. The structure is this: doSomething(); while (someCondition()) { doSomethingElse(); doSomething(); } ...and you want to factor out the doSomething() call so that it only has to be

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

2008-12-04 Thread Aristotle Pagaltzis
* David Green [EMAIL PROTECTED] [2008-12-03 22:00]: FIRST{} can do something on only the first iteration through the loop, but there's no NOT-FIRST block to do something on the second and subsequent iterations. Is there an elegant way to do something on all but the first loop? Not with a

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

2008-12-03 Thread Aristotle Pagaltzis
Hi all, I occasionally find myself annoyed at having to do something like this (I use Perl 5 vernacular, but it actually crops up in every single language I have ever used): my $i; @stuff = grep !$_-valid, @stuff; while ( @stuff ) { $_-do_something( ++$i ) for @stuff;

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

2008-12-03 Thread Bruce Gray
On Dec 3, 2008, at 7:14 AM, Aristotle Pagaltzis wrote: --snip-- Does Perl 6 have some mechanism so I could write it along the following obvious lines? my $i; while ( @stuff ) { $_-do_something( ++$i ) for @stuff; } # plus some way of attaching this fix-up just once

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

2008-12-03 Thread Aristotle Pagaltzis
* Bruce Gray [EMAIL PROTECTED] [2008-12-03 18:20]: In Perl 5 or Perl 6, why not move the grep() into the while()? Because it’s only a figurative example and you’re supposed to consider the general problem, not nitpick the specific example… Regards, -- Aristotle Pagaltzis //

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

2008-12-03 Thread Jon Lang
Aristotle Pagaltzis wrote: * Bruce Gray [EMAIL PROTECTED] [2008-12-03 18:20]: In Perl 5 or Perl 6, why not move the grep() into the while()? Because it's only a figurative example and you're supposed to consider the general problem, not nitpick the specific example… But how is that not a

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

2008-12-03 Thread Mark J. Reed
OK, so let's look at the general problem. The structure is this: doSomething(); while (someCondition()) { doSomethingElse(); doSomething(); } ...and you want to factor out the doSomething() call so that it only has to be specified once. Is that correct, Aristotle? The gotcha is that

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

2008-12-03 Thread Mark J. Reed
On Wed, Dec 3, 2008 at 2:26 PM, Mark J. Reed [EMAIL PROTECTED] wrote: Overall, the goal is to ensure that by the end of the loop the program is in the state of having just called doSomething(), whether the loop runs or not - while also ensuring that the program is in that state at the top

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

2008-12-03 Thread Patrick R. Michaud
On Wed, Dec 03, 2008 at 02:26:57PM -0500, Mark J. Reed wrote: OK, so let's look at the general problem. The structure is this: doSomething(); while (someCondition()) { doSomethingElse(); doSomething(); } ...and you want to factor out the doSomething() call so that it only has

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

2008-12-03 Thread Mark J. Reed
On Wed, Dec 3, 2008 at 3:05 PM, Patrick R. Michaud [EMAIL PROTECTED] wrote: It does seem like a closure trait sort of thing, but I don't think it's currently provided by the p6 spec. Perhaps PRE ... ? Isn't PRE { blah; } just short for ENTER { die unless blah; } ? It still has the problem

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

2008-12-03 Thread mark . a . biggar
loop { doSomething(); next if someCondition(); doSomethingElse(); } -- Mark Biggar [EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED] -- Original message -- From: Mark J. Reed [EMAIL PROTECTED] OK, so let's look at the general problem. The structure

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

2008-12-03 Thread mark . a . biggar
oops make that last if !someCondition(); -- Mark Biggar [EMAIL PROTECTED] [EMAIL PROTECTED] [EMAIL PROTECTED] -- Original message -- From: [EMAIL PROTECTED] loop { doSomething(); next if someCondition(); doSomethingElse(); } -- Mark Biggar

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

2008-12-03 Thread Mark J. Reed
On Wed, Dec 3, 2008 at 3:42 PM, [EMAIL PROTECTED] wrote: loop { doSomething(); next if someCondition(); doSomethingElse(); } That loops forever, doesn't it? But I think this works: loop { doSomething(); last unless someCondition(); doSomethingElse(); } -- Mark J. Reed

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

2008-12-03 Thread Mark J. Reed
On Wed, Dec 3, 2008 at 3:44 PM, Mark J. Reed [EMAIL PROTECTED] wrote: On Wed, Dec 3, 2008 at 3:42 PM, [EMAIL PROTECTED] wrote: loop { doSomething(); next if someCondition(); doSomethingElse(); } That loops forever, doesn't it? But I think this works: loop { doSomething();

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

2008-12-03 Thread David Green
On 2008-Dec-3, at 12:38 pm, Mark J. Reed wrote: Overall, the goal is to ensure that by the end of the loop the program is in the state of having just called doSomething(), whether the loop runs or not - while also ensuring that the program is in that state at the top of each loop iteration.

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

2008-12-03 Thread Jon Lang
Mark J. Reed wrote: Mark J. Reed wrote: loop { doSomething(); last unless someCondition(); doSomethingElse(); } That is, of course, merely the while(1) version from Aristotle's original message rewritten with Perl 6's loop keyword. As I said, I'm OK with that, personally, but

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

2008-12-03 Thread Eirik Berg Hanssen
Mark J. Reed [EMAIL PROTECTED] writes: OK, so let's look at the general problem. The structure is this: doSomething(); while (someCondition()) { doSomethingElse(); doSomething(); } ...and you want to factor out the doSomething() call so that it only has to be specified once.