Thread overview
The limitations of foreach
Aug 03, 2004
Sean Kelly
Aug 03, 2004
Juanjo Álvarez
[Forwarded] Re: The limitations of foreach
Aug 03, 2004
Juanjo Álvarez
Aug 03, 2004
Lord Syl
Re: [Forwarded] The limitations of foreach (edit)
Aug 03, 2004
Lord Syl
Aug 03, 2004
Sean Kelly
Aug 04, 2004
Juanjo Álvarez
Aug 04, 2004
Matthew
August 03, 2004
I've been kicking around how to "D-ify" the STL algorithms and ran into a snag: foreach can only iterate across one range at a time while some of the STL algorithms want to iterate across two simultaneously.  I'm thinking of things like lexicographical_compare and such.  On the surface this seems like a strong argument for iterators, but I'm wondering if there might be a more D-like way to handle these.  Any suggestions?


Sean


August 03, 2004
Sean Kelly wrote:

>  Any suggestions?

Yes, extend the foreach syntax in the natural way (for D 2.0 of course):

int compare(char[] string1, char[] string2)
{
        foreach(char c1, int idx : char c2; string1 : string2)
        {
                if (c1 != c2)return idx;
        }
        return -1;
}

or:

foreach(char c1, int idx; string1 : char c2; string2)
{
}

Maybe the ':' is not the best option here, just using some random char to express the idea.

Of course this would not be limited to two containers :) One problem you can have is: What if string1 is longer than string2? What value do you assign to c2 once it is over the limit?

August 03, 2004
Juanjo Álvarez wrote:

> Sean Kelly wrote:
> 
> Any suggestions?

 Yes, extend the foreach syntax in the natural way (for D 2.0 of course):

 int compare(char[] string1, char[] string2)
 {
         foreach(char c1, int idx : char c2; string1 : string2)
         {
                 if (c1 != c2)return idx;
         }
         return -1;
}

or:

foreach(char c1, int idx; string1 : char c2; string2)
{
}

Maybe the ':' is not the best option here, just using some random char to
express the idea.

Of course this would not be limited to two containers :) One problem you
can have is: What if string1 is longer than string2? What value do you
assign to c2 once it is over the limit?

August 03, 2004
In article <cep3jk$1n3q$1@digitaldaemon.com>, Juanjo =?ISO-8859-15?Q?=C1lvarez?= says...
>
>Juanjo Álvarez wrote:
>
>> Sean Kelly wrote:
>> 
>> Any suggestions?
> 
> Yes, extend the foreach syntax in the natural way (for D 2.0 of course):
> 
> int compare(char[] string1, char[] string2)
> {
>         foreach(char c1, int idx : char c2; string1 : string2)
>         {
>                 if (c1 != c2)return idx;
>         }
>         return -1;
>}
> 
>or:
> 
>foreach(char c1, int idx; string1 : char c2; string2)
>{
>}
> 
>Maybe the ':' is not the best option here, just using some random char to express the idea.
> 
>Of course this would not be limited to two containers :) One problem you can have is: What if string1 is longer than string2? What value do you assign to c2 once it is over the limit?
>

I got it. Good intention, yet the proposed one is much of a clumsy syntax (One
of the reasons that make C++ terrible).
Instead, i'd say that something like this would be more readable:

int compare(char[] string1, char[] string 2)
{
foreach(char [c1 :: c2] , int idx, [string1 :: string2])
{
if (c1!=c2) return idx;
}
return -1;
}

What do you say? :)


August 03, 2004
In article <cep4op$1ngm$1@digitaldaemon.com>, Lord Syl says...
>
>In article <cep3jk$1n3q$1@digitaldaemon.com>, Juanjo =?ISO-8859-15?Q?=C1lvarez?= says...
>>
>>Juanjo Álvarez wrote:
>>
>>> Sean Kelly wrote:
>>> 
>>> Any suggestions?
>> 
>> Yes, extend the foreach syntax in the natural way (for D 2.0 of course):
>> 
>> int compare(char[] string1, char[] string2)
>> {
>>         foreach(char c1, int idx : char c2; string1 : string2)
>>         {
>>                 if (c1 != c2)return idx;
>>         }
>>         return -1;
>>}
>> 
>>or:
>> 
>>foreach(char c1, int idx; string1 : char c2; string2)
>>{
>>}
>> 
>>Maybe the ':' is not the best option here, just using some random char to express the idea.
>> 
>>Of course this would not be limited to two containers :) One problem you can have is: What if string1 is longer than string2? What value do you assign to c2 once it is over the limit?
>>
>
>I got it. Good intention, yet the proposed one is much of a clumsy syntax (One
>of the reasons that make C++ terrible).
>Instead, i'd say that something like this would be more readable:
>
>int compare(char[] string1, char[] string 2)
>{
>foreach(char [c1 :: c2] , int idx, [string1 :: string2])
>{
>if (c1!=c2) return idx;
>}
>return -1;
>}
>
>What do you say? :)
>

Ah, I forgot to say! . If string1 and string2's length were different, the smaller of the two would be picked then, (that's intuitive).


August 03, 2004
In article <cep54e$1nkd$1@digitaldaemon.com>, Lord Syl says...
>
>Ah, I forgot to say! . If string1 and string2's length were different, the smaller of the two would be picked then, (that's intuitive).

I think that would do what I need, though I'm not sure I like the syntax:

# // dumb example of a string comparison
# bit lexical_less( char[] string1, char[] string2 )
# {
#     foreach( char [c1 :: c2], int idx, [string1 :: string2] )
#     {
#         if( c1 < c2 ) return true;
#         if( c1 > c2 ) return false;
#     }
#     return c1.length < c2.length;
# }


Sean


August 04, 2004
"Sean Kelly" <sean@f4.ca> wrote in message news:ceoq1l$1hov$1@digitaldaemon.com...
> I've been kicking around how to "D-ify" the STL algorithms and ran into a snag: foreach can only iterate across one range at a time while some of the STL algorithms want to iterate across two simultaneously.  I'm thinking of things like lexicographical_compare and such.  On the surface this seems like a strong argument for iterators, but I'm wondering if there might be a more D-like way to handle these.  Any suggestions?

Glad to see you're running into the same thoughts I'd had on that one.

I'm currently wearing two hats on this one. On the one hand, I tend to be in the Walter camp, in so far as I think D
generics will be different from C++ generics, and we should not worry too much about that. I must concede that I very
rarely use STL algorithms other than copy(), find(), for_each() and accumulate().

On the other, I believe that DTL can probably support such things - although it may have to wait for D 1.0+ language features - and therefore it _should_ support them.

For my part, I'm putting iterator-based stuff fourth on my Enumeration To-Do list, behind foreach, interfaces and ranges (composable foreach). That way, there'll be plenty of scope for finding out if there is a D way, and I should also be able to piggy-back on all you smart folks that're trying to do the STL way. <g>


Having said all that, however, I find it incredibly irritating not to be able to conduct two concurrent enumerations with the same construct. This should be fixed, somehow.



August 04, 2004
Lord Syl wrote:

> Instead, i'd say that something like this would be more readable:
> 
> int compare(char[] string1, char[] string 2)
> {
> foreach(char [c1 :: c2] , int idx, [string1 :: string2])
> {
> if (c1!=c2) return idx;
> }
> return -1;
> }
> 
> What do you say? :)

I like it, but let's see if people can come with a still better one :)

Walter, what do you think of the feature (not the syntax), would it be very
hard to implement?