Discussion:
C++ Ranges proposal for the Standard Library
(too old to reply)
ZombineDev via Digitalmars-d
2014-10-17 09:17:51 UTC
Permalink
I saw [this][0] proposal for adding ranges to C++'s standard
Since iterators can implement D ranges, but D ranges cannot be
used to implement iterators, we conclude that iterators form a
more powerful and foundational basis.
What do you guys think?

[0]: https://isocpp.org/blog/2014/10/ranges
[1]: https://ericniebler.github.io/std/wg21/D4128.html
Marco Leise via Digitalmars-d
2014-10-17 10:01:40 UTC
Permalink
Am Fri, 17 Oct 2014 09:17:51 +0000
Post by ZombineDev via Digitalmars-d
I saw [this][0] proposal for adding ranges to C++'s standard
Since iterators can implement D ranges, but D ranges cannot be
used to implement iterators, we conclude that iterators form a
more powerful and foundational basis.
What do you guys think?
[0]: https://isocpp.org/blog/2014/10/ranges
[1]: https://ericniebler.github.io/std/wg21/D4128.html
True. Iterators are more foundational, ranges are more
neat-o. ;)
When you look at this C++ function as part of a signal
processing home work you know why you don't want to see them
in top level code:

// C++

double mittelwert(const vector<double>& vektor)
{
vector<double>::const_iterator it;
double summe = 0;
for (it = vektor.begin(); it != vektor.end(); ++it)
{
summe += *it;
}
return summe / vektor.size();
}

// D (removing iterators)

double mittelwert(in double[] vektor)
{
double summe = 0;
foreach (wert; vektor)
{
summe += wert;
}
return summe / vektor.length;
}

// D (using range sum function)

double mittelwert(in double[] vektor)
{
return sum(vektor) / vektor.length;
}
--
Marco
Olivier Grant via Digitalmars-d
2014-10-17 10:10:21 UTC
Permalink
Post by Marco Leise via Digitalmars-d
Am Fri, 17 Oct 2014 09:17:51 +0000
Post by ZombineDev via Digitalmars-d
I saw [this][0] proposal for adding ranges to C++'s standard
Since iterators can implement D ranges, but D ranges cannot
be used to implement iterators, we conclude that iterators
form a more powerful and foundational basis.
What do you guys think?
[0]: https://isocpp.org/blog/2014/10/ranges
[1]: https://ericniebler.github.io/std/wg21/D4128.html
True. Iterators are more foundational, ranges are more
neat-o. ;)
When you look at this C++ function as part of a signal
processing home work you know why you don't want to see them
// C++
double mittelwert(const vector<double>& vektor)
{
vector<double>::const_iterator it;
double summe = 0;
for (it = vektor.begin(); it != vektor.end(); ++it)
{
summe += *it;
}
return summe / vektor.size();
}
// D (removing iterators)
double mittelwert(in double[] vektor)
{
double summe = 0;
foreach (wert; vektor)
{
summe += wert;
}
return summe / vektor.length;
}
// D (using range sum function)
double mittelwert(in double[] vektor)
{
return sum(vektor) / vektor.length;
}
No, the equivalent implementation in C++ is this:

double mittelwert(const vector<double>& vektor)
{
double summe = 0;

for(auto &x : vektor)
{
summe += x;
}

return summe / vektor.size();
}
eles via Digitalmars-d
2014-10-17 10:17:37 UTC
Permalink
Post by Marco Leise via Digitalmars-d
Am Fri, 17 Oct 2014 09:17:51 +0000
double mittelwert_accumulate(const vector<double>& vektor)
{
return accumulate(vektor.begin(), vektor.end(), 0.0) /
vektor.size();
}

Continue reading on narkive:
Loading...