[OT] foreach(scheme) (Was: Differentiate const flavors using CASE?)

Daniel Keep daniel.keep.lists at gmail.com
Wed Mar 21 22:22:42 PDT 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/



More information about the Digitalmars-d mailing list