map! filter! and range algorithm

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Mar 5 22:40:52 PST 2013


On Wed, Mar 06, 2013 at 04:25:33AM +0100, bearophile wrote:
[...]
> Stepanov himself has commented very briefly about Alexandrescu/D
> Ranges in one of his video lessons. I have not fully understood what
> Stepanov has said with those few words spoken in a not so good
> English (not because of language, but because Stepanov is still out
> of my league, he's a Teacher) but I think he has dismissed the
> Ranges. A bit like a chemist would dismiss a person that says that
> molecules are more important than atoms and only molecules should be
> used.
> 
> I think he has said that atoms are there (in a kind of mathematical
> Platonism), even if you try to ignore them, they are more
> fundamental. So Ranges are a little abstraction over C++-style
> iterators. They are maybe more handy, safer, they allow you to
> reason at a a bit higher lever, but they also forbid you to do few
> of the useful things that iterators can do.
> 
> In the end I think ranges were a success for D. Almost every part of
> the design of D has problems; sometimes even large problems (like
> shared, problems with missing head and tail const, missing logic
> constness, problems with scope, and so on at nauseum), but most
> times D ranges work, despite some faults they have.
[...]

I have used both C++ iterators and D ranges, and I have to say that even
if some things possible with iterators are not possible with ranges, the
concept of ranges has made std.algorithm/std.range so much easier to
use. Consider how you would write code with composed ranges using C++
iterators. In D, because of the range abstraction, you can simply chain
them in a logical manner; in C++, the fact that most algorithms need an
iterator *pair* means chaining is very painful. You have to constantly
keep track of which two iterators go together. Chaining requires
wrappers to group iterator pairs together, unpack them when calling the
next function, regroup them afterwards, etc.. Sure, everything is
*possible* to do with iterators -- perhaps even more than ranges. But
ranges are a much more convenient way to reason about and write these
things, and the resulting code is much easier to read.


T

-- 
"Real programmers can write assembly code in any language. :-)" -- Larry Wall


More information about the Digitalmars-d-learn mailing list