www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dtl - The limitations of foreach

reply Sean Kelly <sean f4.ca> writes:
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
Aug 03 2004
next sibling parent Juanjo =?ISO-8859-15?Q?=C1lvarez?= <juanjuxNO SPAMyahoo.es> writes:
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?
Aug 03 2004
prev sibling parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"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.
Aug 03 2004