Super-dee-duper D features
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Tue Feb 13 08:10:54 PST 2007
Bill Baxter wrote:
> janderson wrote:
>> Andrei Alexandrescu (See Website For Email) wrote:
>>> James Dennett wrote:
>>>> C++, of course, has std::for_each(e.begin(), e.end(), do_x);
>>>> in its library (though that's weaker than it could be because
>>>> of lack of support for convenient anonymous functions/lambdas).
>>>>
>>>> C++0x is very likely to have for(v: e). It's implemented
>>>> in ConceptGCC already. Java already has essentially that,
>>>> as does C#. This really doesn't set D apart (but at least
>>>> D isn't falling behind here).
>>>
>>> BTW, D might soon have simultaneous iteration that will blow away all
>>> conventional languages:
>>>
>>> foreach (i ; coll1) (j ; coll2)
>>> {
>>> ... use i and j ...
>>> }
>>> continue foreach (i)
>>> {
>>> ... coll2 finished; use i ...
>>> }
>>> continue foreach (j)
>>> {
>>> ... coll1 finished; use j ...
>>> }
>>>
>>> Andrei
>>
>> Maybe its a bit to late here however this syntax seems very special
>> case. Can you explain why its necessary and how we would use it. How
>> would we do this currently (without meta programming)?
>>
>
> Yeh, I don't get it either. How would that help me implement merge()
> from merge sort for instance?
Merge bumps the iteration in both collections conditionally. The form
above bumps the iteration in the two collections unconditionally, until
one is finished; then it continues with the other until that is finished.
Say you need to compute the minimum and maximum element for each column
in a file. The code (translated from Perl) looks something like this
(simplified):
foreach (row ; rows) {
for (int i = 0; i != cols; ++i) {
if (mins[i] > row[i]) mins[i] = row[i];
if (maxs[i] < row[i]) maxs[i] = row[i];
}
}
What you'd rather do is to simultaneously iterate row, mins, and maxs:
foreach (row ; rows) {
foreach (e ; row) (inout min ; mins) (inout max ; maxs) {
if (min > e) min = e;
if (max < e) max = e;
}
}
Andrei
More information about the Digitalmars-d
mailing list