August 05, 2005 Re: foreach annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | I dunno what the poster's intent was.
On Fri, 2005-08-05 at 00:05 +0000, AJG wrote:
> Hi,
>
> >import std.stdio;
> >void main() {
> > char[] string = "abcdfgh";
> > foreach(char c; string)
> > {
> > if (c == 'e') goto found;
> > }
> > writefln("not found");
> > found:
> > writefln("done");
> >}
>
> I'm guessing you wanna throw a return in there. Otherwise the "not found" case also reaches the "found" case. No?
>
> --AJG.
>
>
|
August 05, 2005 Re: foreach annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG |
> Fantastic! This would be simply awesome. It would pave the way for the
> expressionisation of all constructs just like current GNU C extensions
> allow. A
> vote for expression-foreach is a vote for the future.
Holy smokes. I just read through the GNU C extension list. Those guys make
Willy Wonka look like an amateur :P
I wonder what feedback they've gotten about some of those ideas.
|
August 05, 2005 Re: foreach annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Hi, >> Fantastic! This would be simply awesome. It would pave the way for the >> expressionisation of all constructs just like current GNU C extensions >> allow. A >> vote for expression-foreach is a vote for the future. > >Holy smokes. I just read through the GNU C extension list. Those guys make Willy Wonka look like an amateur :P Yes indeed. When I started using GNU C (and the exts) I became spoiled for life. Going back to even C99 is a painful proposition. >I wonder what feedback they've gotten about some of those ideas. I suspect it's been mostly positive for two reasons: a) They are all toggleable (specifically or all at once). So you can use them or completely ignore them. You can also force ANSI/C89/99 compatibility if you want. b) They keep churning 'em out. ;) I really, really hope Walter takes a look at them and invests time in implementing the best of them in D. A particularly easy one I suggested a while back was omitting the middle operand in the ternary operator. Expression-foreach would be a great step in that direction, so I definitely support it. Cheers, --AJG. |
August 09, 2005 Re: foreach annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle |
How about foreach...else statement? I think it is an elegant solution.
On Fri, 05 Aug 2005 00:57:32 +1100, Ben Hinkle <bhinkle@mathworks.com> wrote:
>> But it would be nice if there was a more elegant solution. Something like:
>>
>> foreach(char c; string)
>> {
>> if(c == 'e') break;
>> }
>> overflow
>> {
>> //not found
>> }
>
> Another option is to make foreach return the value of the opApply (or the
> builtin loop if there is no opApply) and turn foreach into an expression
> instead of a statement. So for example
> int found = foreach(char c; string){
> if (c == 'e') break;
> };
> if (found) {
> // found 'e' in string
> } else {
> //. not found case
> }
>
> Note making foreach an expression would break existing code since the
> trailing ; would be needed to turn the expression into a statement. It also
> might be confusing that people might think they could have "return"
> statements in the foreach body instead of "break". People would also
> probably want to return values from the foreach like the index at the break.
>
> Given those problems if making foreach an expression is too wacky the result
> can be stored in an implicit variable _foreachAborted or something:
> foreach(char c; string){
> if (c == 'e') break;
> }
> if (_foreachAborted) {
> // found 'e' in string
> } else {
> //. not found case
> }
>
>
|
August 09, 2005 Re: foreach annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Zaitseff | On Tue, 09 Aug 2005 13:30:23 +1100, Ilya Zaitseff <sark7@mail333.com> wrote: > How about foreach...else statement? I think it is an elegant solution. To me 'else' implies "if there is nothing in the container" whereas I want "if you reach the end of the container", having else do what I want would be confusing.. or maybe just to me :) Regan > On Fri, 05 Aug 2005 00:57:32 +1100, Ben Hinkle <bhinkle@mathworks.com> wrote: >>> But it would be nice if there was a more elegant solution. Something like: >>> >>> foreach(char c; string) >>> { >>> if(c == 'e') break; >>> } >>> overflow >>> { >>> //not found >>> } >> >> Another option is to make foreach return the value of the opApply (or the >> builtin loop if there is no opApply) and turn foreach into an expression >> instead of a statement. So for example >> int found = foreach(char c; string){ >> if (c == 'e') break; >> }; >> if (found) { >> // found 'e' in string >> } else { >> //. not found case >> } >> >> Note making foreach an expression would break existing code since the >> trailing ; would be needed to turn the expression into a statement. It also >> might be confusing that people might think they could have "return" >> statements in the foreach body instead of "break". People would also >> probably want to return values from the foreach like the index at the break. >> >> Given those problems if making foreach an expression is too wacky the result >> can be stored in an implicit variable _foreachAborted or something: >> foreach(char c; string){ >> if (c == 'e') break; >> } >> if (_foreachAborted) { >> // found 'e' in string >> } else { >> //. not found case >> } >> >> > |
August 09, 2005 Re: foreach annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | In article <opsu7ymedr23k2f5@nrage.netwin.co.nz>, Regan Heath says... > >On Tue, 09 Aug 2005 13:30:23 +1100, Ilya Zaitseff <sark7@mail333.com> wrote: >> How about foreach...else statement? I think it is an elegant solution. > >To me 'else' implies "if there is nothing in the container" whereas I want "if you reach the end of the container", having else do what I want would be confusing.. or maybe just to me :) No, I agree. My suggestion suggestion a while ago interpreted it just like that. foreach (item; array) // Array not empty. else // Array is empty. Cheers, --AJG. > >Regan > >> On Fri, 05 Aug 2005 00:57:32 +1100, Ben Hinkle <bhinkle@mathworks.com> wrote: >>>> But it would be nice if there was a more elegant solution. Something like: >>>> >>>> foreach(char c; string) >>>> { >>>> if(c == 'e') break; >>>> } >>>> overflow >>>> { >>>> //not found >>>> } >>> >>> Another option is to make foreach return the value of the opApply (or >>> the >>> builtin loop if there is no opApply) and turn foreach into an expression >>> instead of a statement. So for example >>> int found = foreach(char c; string){ >>> if (c == 'e') break; >>> }; >>> if (found) { >>> // found 'e' in string >>> } else { >>> //. not found case >>> } >>> >>> Note making foreach an expression would break existing code since the >>> trailing ; would be needed to turn the expression into a statement. It >>> also >>> might be confusing that people might think they could have "return" >>> statements in the foreach body instead of "break". People would also >>> probably want to return values from the foreach like the index at the >>> break. >>> >>> Given those problems if making foreach an expression is too wacky the >>> result >>> can be stored in an implicit variable _foreachAborted or something: >>> foreach(char c; string){ >>> if (c == 'e') break; >>> } >>> if (_foreachAborted) { >>> // found 'e' in string >>> } else { >>> //. not found case >>> } >>> >>> >> > |
August 09, 2005 Re: foreach annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to AJG | On Tue, 9 Aug 2005 04:15:38 +0000 (UTC), AJG wrote: > foreach (item; array) > // Array not empty. > else > // Array is empty. This would be useful, as it saves me declaring a loop index just for checking the first or last iteration, which is something I do frequently, ... foreach( <itemdecl>; <arrayref> ) on first <statement> // First iteration only on last <statement> // Final iteration only else <statement> // Only when there are zero iterations Example: foreach( CustRec c; CustomerList ) on first { Lineno = 0; Totals = 0; Pageno = 0; InitializeReport(); } Lineno++; GenPageHeading(Lineno, Pageno); GenDetailLine(Lineno, c, Totals); on last { GenFooter(Totals); CloseReport(); } else Msg("No customers on file"); Not a big thing, just a nice-to-have. -- Derek Melbourne, Australia 9/08/2005 2:28:36 PM |
August 09, 2005 Re: foreach annoyance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Hi, >> foreach (item; array) >> // Array not empty. >> else >> // Array is empty. > >This would be useful, as it saves me declaring a loop index just for checking the first or last iteration, which is something I do frequently, Not a big thing, just a nice-to-have. Then, by all means, vote for it*: http://www.all-technology.com/eigenpolls/dwishlist/ * Although there hasn't been shown a positive correlation between the D wishlist and the D language. It functions more like a salve. ;) Cheers, --AJG. |
Copyright © 1999-2021 by the D Language Foundation