March 22, 2007
janderson wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
> 
>> Derek Parnell wrote:
>>
>>> On Thu, 22 Mar 2007 04:53:26 +0900, Bill Baxter wrote:
>>>
>>>> Here's a random thought:
>>>> What about const vs CONST?
>>>> The upcase version obviously being the more const of the two.
>>>> The original proposal played with punctuation, and we've talked plenty about spelling, but we haven't talked about playing with case.  It would be an odd-ball among keywords, admittedly, but if you asked 100 people which of 'const' and 'CONST' was the most constant you'd probably get 100 votes for 'CONST'.  And he could become good friends with foreach_reverse, the other odd-ball keyword who is disparaged by the other kids because of his obesity and the big staple in his belly button.
>>>
>>>
>>> LOL ... Now that *is* funny.
>>
>>
>> Yah :o). Speaking of foreach_reverse, probably it would be wise to lobby Walter to deprecate it in favor of foreach(reverse) (item ; collection) { ... }. The keyword(extra) syntax is definitely becoming a D signature syntax.
>>
>>
>> Andrei
> 
> 
> //Using your other suggestion:
> foreach(reverse) (item ; collection) (item2 ; x->GetColection(b)) (item3 ; collection3)
> {
> 
> }
> 
> Its starting to get hard and harder to read IMO.
> 
> Although perhaps the reverse could be come sort of iterator mechanism. You could define what order items are visited.  I mean, reverse would not be a keyword at all and would exist in some library.  Although I'm not sure how it would be implemented, and it may defeat the purpose of foreach_reverse being optimal.
> 
> Just a thought.

shouldn't this perhaps be something like:

foreach (item; collection) (item2; x->GetColection(b))(item3; collection3.reverse)
{

}


Now you can select a direction on the each container?

Naturally, this would avoid performing a copy of the content, and the basic approach is quite easy to handle via a struct containing an opApply(). It would open the door to alternate iteration approaches also, such as skipping entries, merging, or whatever.

Tango does something vaguely like this for the text-iterators in tango.text.Util
March 22, 2007
Derek Parnell wrote:
> On Wed, 21 Mar 2007 17:33:00 -0700, Benji Smith wrote:
> 
>> Andrei Alexandrescu (See Website For Email) wrote:
>>> Derek Parnell wrote:
>>>> On Thu, 22 Mar 2007 04:53:26 +0900, Bill Baxter wrote:
>>>>
>>>>> Here's a random thought:
>>>>> What about const vs CONST?
>>>>> The upcase version obviously being the more const of the two.
>>>>> The original proposal played with punctuation, and we've talked plenty about spelling, but we haven't talked about playing with case.  It would be an odd-ball among keywords, admittedly, but if you asked 100 people which of 'const' and 'CONST' was the most constant you'd probably get 100 votes for 'CONST'.  And he could become good friends with foreach_reverse, the other odd-ball keyword who is disparaged by the other kids because of his obesity and the big staple in his belly button.
>>>> LOL ... Now that *is* funny.
>>> Yah :o). Speaking of foreach_reverse, probably it would be wise to lobby Walter to deprecate it in favor of foreach(reverse) (item ; collection) { ... }. The keyword(extra) syntax is definitely becoming a D signature syntax.
>>>
>>> Andrei
>> What do you call that little non-keyword in parens when you refer to it in your parsing code? If it's not a keyword or an operator or an identifier, how do you refer to it?
>>
>> Just curious.
> 
> An adornment/ornamentation/embellishment maybe? 

Or, to annoy people who hate Microsoft's "musical" languages, we could call them "grace notes".

Dave
March 22, 2007

kris wrote:
> janderson wrote:
>> Andrei Alexandrescu (See Website For Email) wrote:
>>>
>>>
>>> Yah :o). Speaking of foreach_reverse, probably it would be wise to
>>> lobby Walter to deprecate it in favor of foreach(reverse) (item ;
>>> collection) { ... }. The keyword(extra) syntax is definitely becoming
>>> a D signature syntax.
>>>
>>>
>>> Andrei
>>
>>
>> //Using your other suggestion:
>> foreach(reverse) (item ; collection) (item2 ; x->GetColection(b))
>> (item3 ; collection3)
>> {
>>
>> }
>>
>> Its starting to get hard and harder to read IMO.
>>
>> Although perhaps the reverse could be come sort of iterator mechanism. You could define what order items are visited.  I mean, reverse would not be a keyword at all and would exist in some library.  Although I'm not sure how it would be implemented, and it may defeat the purpose of foreach_reverse being optimal.
>>
>> Just a thought.
> 
> shouldn't this perhaps be something like:
> 
> foreach (item; collection) (item2; x->GetColection(b))(item3;
> collection3.reverse)
> {
> 
> }
> 
> 
> Now you can select a direction on the each container?
> 
> Naturally, this would avoid performing a copy of the content, and the basic approach is quite easy to handle via a struct containing an opApply(). It would open the door to alternate iteration approaches also, such as skipping entries, merging, or whatever.
> 
> Tango does something vaguely like this for the text-iterators in tango.text.Util

What if collection3 is a 40 MB array?

The nice thing about foreach_reverse is that it doesn't require any transformations on the data.  The bad thing is... it's ugly :P  That, and once we get parallel iteration, we won't be able to go forward on one aggregate, and backwards on the other.

Possibilities:

foreach (forward,forward,reverse) (item1; col1) (item2; x.GetCol(b))
	(item3; col3)
{
    // ...
}

foreach (item1; col) (item2; x.GetCol(b)) (reverse)(item3; col3)
{
    // ...
}

foreach (item1; col) (item2; x.GetCol(b)) (reverse item3; col3)
{
    // ...
}

In any case, I like the idea of having foreach(scheme), where "scheme" could be "forward", "reverse", "depthfirst", "topological", whatever. And if D2.0 gets generators as well... well, I can't think of any other procedural language that could even come close to touching us :)

	-- Daniel

P.S. One last thought:

class Heap
{
    int opApply(...) { ... dg(v); ... }
    int opApplyReverse(...) { ... }
    int opApply_depthfirst(...) { ... }
    int opApply_sorted(...) { ... }

    // Wouldn't it be cool if the above could be written as...

    foreach(...) { foreach( v ; this.backing ) yield v; }
    foreach(reverse)(...) { ... }
    foreach(depthfirst)(...) { ... }
    foreach(sorted)(...) { ... }
}

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/
March 22, 2007
Daniel Keep wrote:
> 
> kris wrote:
> 
>>janderson wrote:
>>
>>>Andrei Alexandrescu (See Website For Email) wrote:
>>>
>>>>
>>>>Yah :o). Speaking of foreach_reverse, probably it would be wise to
>>>>lobby Walter to deprecate it in favor of foreach(reverse) (item ;
>>>>collection) { ... }. The keyword(extra) syntax is definitely becoming
>>>>a D signature syntax.
>>>>
>>>>
>>>>Andrei
>>>
>>>
>>>//Using your other suggestion:
>>>foreach(reverse) (item ; collection) (item2 ; x->GetColection(b))
>>>(item3 ; collection3)
>>>{
>>>
>>>}
>>>
>>>Its starting to get hard and harder to read IMO.
>>>
>>>Although perhaps the reverse could be come sort of iterator mechanism.
>>>You could define what order items are visited.  I mean, reverse would
>>>not be a keyword at all and would exist in some library.  Although I'm
>>>not sure how it would be implemented, and it may defeat the purpose of
>>>foreach_reverse being optimal.
>>>
>>>Just a thought.
>>
>>shouldn't this perhaps be something like:
>>
>>foreach (item; collection) (item2; x->GetColection(b))(item3;
>>collection3.reverse)
>>{
>>
>>}
>>
>>
>>Now you can select a direction on the each container?
>>
>>Naturally, this would avoid performing a copy of the content, and the
>>basic approach is quite easy to handle via a struct containing an
>>opApply(). It would open the door to alternate iteration approaches
>>also, such as skipping entries, merging, or whatever.
>>
>>Tango does something vaguely like this for the text-iterators in
>>tango.text.Util
> 
> 
> What if collection3 is a 40 MB array?
> 
> The nice thing about foreach_reverse is that it doesn't require any
> transformations on the data.  

I didn't suggest transformation at all :) Merely a mean of satisfying opApply that happens to go backwards instead (or some other pattern).

Please, take a peek at tango.text.Util ... methods lines(), delimiters() and patterns() are examples of what I'm getting at. That particular approach can easily be adapted for any kind of container
March 22, 2007
Daniel Keep wrote:
 > In any case, I like the idea of having foreach(scheme), where "scheme"
> could be "forward", "reverse", "depthfirst", "topological", whatever. And if D2.0 gets generators as well... well, I can't think of any other procedural language that could even come close to touching us :)
> 
> 	-- Daniel
> 
> P.S. One last thought:
> 
> class Heap
> {
>     int opApply(...) { ... dg(v); ... }
>     int opApplyReverse(...) { ... }
>     int opApply_depthfirst(...) { ... }
>     int opApply_sorted(...) { ... }
> 
>     // Wouldn't it be cool if the above could be written as...
> 
>     foreach(...) { foreach( v ; this.backing ) yield v; }
>     foreach(reverse)(...) { ... }
>     foreach(depthfirst)(...) { ... }
>     foreach(sorted)(...) { ... }
> }
> 

That /would/ be cool. I think a syntax like "foreach (delegate) (index, data;
collection)" instead of the current "foreach (index, data;
&delegate(collection))" would be really nice.
March 22, 2007
Derek Parnell wrote:
> On Wed, 21 Mar 2007 17:33:00 -0700, Benji Smith wrote:
> 
>> Andrei Alexandrescu (See Website For Email) wrote:
>>> Derek Parnell wrote:
>>>> On Thu, 22 Mar 2007 04:53:26 +0900, Bill Baxter wrote:
>>>>
>>>>> Here's a random thought:
>>>>> What about const vs CONST?
>>>>> The upcase version obviously being the more const of the two.
>>>>> The original proposal played with punctuation, and we've talked plenty about spelling, but we haven't talked about playing with case.  It would be an odd-ball among keywords, admittedly, but if you asked 100 people which of 'const' and 'CONST' was the most constant you'd probably get 100 votes for 'CONST'.  And he could become good friends with foreach_reverse, the other odd-ball keyword who is disparaged by the other kids because of his obesity and the big staple in his belly button.
>>>> LOL ... Now that *is* funny.
>>> Yah :o). Speaking of foreach_reverse, probably it would be wise to lobby Walter to deprecate it in favor of foreach(reverse) (item ; collection) { ... }. The keyword(extra) syntax is definitely becoming a D signature syntax.
>>>
>>> Andrei
>> What do you call that little non-keyword in parens when you refer to it in your parsing code? If it's not a keyword or an operator or an identifier, how do you refer to it?
>>
>> Just curious.
> 
> An adornment/ornamentation/embellishment maybe? 
> 

I've always thought of them as "identifier/identity parameters"... but I like "adornment" better.

-- Chris Nicholson-Sauls
March 22, 2007
kris wrote:
> janderson wrote:
>> Andrei Alexandrescu (See Website For Email) wrote:
>>
>>> Derek Parnell wrote:
>>>
>>>> On Thu, 22 Mar 2007 04:53:26 +0900, Bill Baxter wrote:
>>>>
>>>>> Here's a random thought:
>>>>> What about const vs CONST?
>>>>> The upcase version obviously being the more const of the two.
>>>>> The original proposal played with punctuation, and we've talked plenty about spelling, but we haven't talked about playing with case.  It would be an odd-ball among keywords, admittedly, but if you asked 100 people which of 'const' and 'CONST' was the most constant you'd probably get 100 votes for 'CONST'.  And he could become good friends with foreach_reverse, the other odd-ball keyword who is disparaged by the other kids because of his obesity and the big staple in his belly button.
>>>>
>>>>
>>>> LOL ... Now that *is* funny.
>>>
>>>
>>> Yah :o). Speaking of foreach_reverse, probably it would be wise to lobby Walter to deprecate it in favor of foreach(reverse) (item ; collection) { ... }. The keyword(extra) syntax is definitely becoming a D signature syntax.
>>>
>>>
>>> Andrei
>>
>>
>> //Using your other suggestion:
>> foreach(reverse) (item ; collection) (item2 ; x->GetColection(b)) (item3 ; collection3)
>> {
>>
>> }
>>
>> Its starting to get hard and harder to read IMO.
>>
>> Although perhaps the reverse could be come sort of iterator mechanism. You could define what order items are visited.  I mean, reverse would not be a keyword at all and would exist in some library.  Although I'm not sure how it would be implemented, and it may defeat the purpose of foreach_reverse being optimal.
>>
>> Just a thought.
> 
> shouldn't this perhaps be something like:
> 
> foreach (item; collection) (item2; x->GetColection(b))(item3; collection3.reverse)
> {
> 
> }
> 
> 
> Now you can select a direction on the each container?
> 
> Naturally, this would avoid performing a copy of the content, and the basic approach is quite easy to handle via a struct containing an opApply(). It would open the door to alternate iteration approaches also, such as skipping entries, merging, or whatever.
> 
> Tango does something vaguely like this for the text-iterators in tango.text.Util

I agree.

Walter posted about this in another thread saying foreach_reverse was that way because of some performance nitch.  I still think that foreach_reverse is not the right way to do a reverse.  I think having it as a member or free function is the best.  The compiler should be able to detect .reverses on primitive arrays and optimize them.

Besides, how does the compiler currently optimize something written as an opApply?
March 22, 2007

kris wrote:
> janderson wrote:
>> Andrei Alexandrescu (See Website For Email) wrote:
>>
>>> Derek Parnell wrote:
>>>
>>>> On Thu, 22 Mar 2007 04:53:26 +0900, Bill Baxter wrote:
>>>>
>>>>> Here's a random thought:
>>>>> What about const vs CONST?
>>>>> The upcase version obviously being the more const of the two.
>>>>> The original proposal played with punctuation, and we've talked plenty about spelling, but we haven't talked about playing with case.  It would be an odd-ball among keywords, admittedly, but if you asked 100 people which of 'const' and 'CONST' was the most constant you'd probably get 100 votes for 'CONST'.  And he could become good friends with foreach_reverse, the other odd-ball keyword who is disparaged by the other kids because of his obesity and the big staple in his belly button.
>>>>
>>>>
>>>> LOL ... Now that *is* funny.
>>>
>>>
>>> Yah :o). Speaking of foreach_reverse, probably it would be wise to lobby Walter to deprecate it in favor of foreach(reverse) (item ; collection) { ... }. The keyword(extra) syntax is definitely becoming a D signature syntax.
>>>
>>>
>>> Andrei
>>
>>
>> //Using your other suggestion:
>> foreach(reverse) (item ; collection) (item2 ; x->GetColection(b)) (item3 ; collection3)
>> {
>>
>> }
>>
>> Its starting to get hard and harder to read IMO.
>>
>> Although perhaps the reverse could be come sort of iterator mechanism. You could define what order items are visited.  I mean, reverse would not be a keyword at all and would exist in some library.  Although I'm not sure how it would be implemented, and it may defeat the purpose of foreach_reverse being optimal.
>>
>> Just a thought.
> 
> shouldn't this perhaps be something like:
> 
> foreach (item; collection) (item2; x->GetColection(b))(item3; collection3.reverse)
> {
> 
> }
> 

Or better still, if we wanted to keep 'reverse' away from the collection:

foreach (item; collection) (item2; x->GetColection(b)) (item3; collection3; reverse) {
}

I wouldn't be above a 'foreach(i,x ; source ; order)' syntax in general, really.

-- Chris Nicholson-Sauls
March 22, 2007
Andrei Alexandrescu (See Website For Email) wrote:
> Derek Parnell wrote:
>> On Thu, 22 Mar 2007 04:53:26 +0900, Bill Baxter wrote:
>>
>>> Here's a random thought:
>>> What about const vs CONST?
>>> The upcase version obviously being the more const of the two.
>>> The original proposal played with punctuation, and we've talked plenty about spelling, but we haven't talked about playing with case.  It would be an odd-ball among keywords, admittedly, but if you asked 100 people which of 'const' and 'CONST' was the most constant you'd probably get 100 votes for 'CONST'.  And he could become good friends with foreach_reverse, the other odd-ball keyword who is disparaged by the other kids because of his obesity and the big staple in his belly button.
>>
>> LOL ... Now that *is* funny.
> 
> Yah :o). Speaking of foreach_reverse, probably it would be wise to lobby Walter to deprecate it in favor of foreach(reverse) (item ; collection) { ... }. The keyword(extra) syntax is definitely becoming a D signature syntax.
> 
> 
> Andrei

I guess one extension to this syntax could be constant iterations (the values in the array don't change:

foreach (const) (item ; collection)
{

}

I still think

const foreach (item; collection)
{

}

is better if we did have a feature like this.
March 22, 2007
Chris Nicholson-Sauls wrote:
> 
> 
> kris wrote:
> 
>> janderson wrote:
>>
>>> Andrei Alexandrescu (See Website For Email) wrote:
>>>
>>>> Derek Parnell wrote:
>>>>
>>>>> On Thu, 22 Mar 2007 04:53:26 +0900, Bill Baxter wrote:
>>>>>
>>>>>> Here's a random thought:
>>>>>> What about const vs CONST?
>>>>>> The upcase version obviously being the more const of the two.
>>>>>> The original proposal played with punctuation, and we've talked plenty about spelling, but we haven't talked about playing with case.  It would be an odd-ball among keywords, admittedly, but if you asked 100 people which of 'const' and 'CONST' was the most constant you'd probably get 100 votes for 'CONST'.  And he could become good friends with foreach_reverse, the other odd-ball keyword who is disparaged by the other kids because of his obesity and the big staple in his belly button.
>>>>>
>>>>>
>>>>>
>>>>> LOL ... Now that *is* funny.
>>>>
>>>>
>>>>
>>>> Yah :o). Speaking of foreach_reverse, probably it would be wise to lobby Walter to deprecate it in favor of foreach(reverse) (item ; collection) { ... }. The keyword(extra) syntax is definitely becoming a D signature syntax.
>>>>
>>>>
>>>> Andrei
>>>
>>>
>>>
>>> //Using your other suggestion:
>>> foreach(reverse) (item ; collection) (item2 ; x->GetColection(b)) (item3 ; collection3)
>>> {
>>>
>>> }
>>>
>>> Its starting to get hard and harder to read IMO.
>>>
>>> Although perhaps the reverse could be come sort of iterator mechanism. You could define what order items are visited.  I mean, reverse would not be a keyword at all and would exist in some library.  Although I'm not sure how it would be implemented, and it may defeat the purpose of foreach_reverse being optimal.
>>>
>>> Just a thought.
>>
>>
>> shouldn't this perhaps be something like:
>>
>> foreach (item; collection) (item2; x->GetColection(b))(item3; collection3.reverse)
>> {
>>
>> }
>>
> 
> Or better still, if we wanted to keep 'reverse' away from the collection:
> 
> foreach (item; collection) (item2; x->GetColection(b)) (item3; collection3; reverse) {
> }
> 
> I wouldn't be above a 'foreach(i,x ; source ; order)' syntax in general, really.
> 
> -- Chris Nicholson-Sauls

Except for one thing, Chris; basing the syntax on 'keywords' limits one to what the compiler chooses to support? Basing the opApply() selection on a method/property name instead (with optional arguments also) leaves the door wide open for any implementation you can imagine?