[Issue 8946] « any » function does not what it should do

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Nov 4 04:26:56 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8946



--- Comment #11 from Jonathan M Davis <jmdavisProg at gmx.com> 2012-11-04 04:26:54 PST ---
C++'s find takes a pair of iterators indicating the beginning and ending of a
range and returns an iterator to the element found - i.e. the new beginning of
the range. D's ranges can be viewed as being the same as that pair of
iterators. But unlike iterators, they always have a begin and end. So,
rathering than just return the new begin (which it can't do, because that would
be one iterator instead of the pair), D's find returns the new begin and the
end together as a new range. What D is doing is the logical progression of what
happens when you move from iterators to ranges.

You're free to not like it, but it works _very_ well, and it's very much in
line with how C++ does things, only we're doing it with ranges rather than
iterators, and the result is actually much more powerful than what C++ has,
because ranges are far more powerful.

As for returning an index, that doesn't work for D any more than it works for
C++. For anything which isn't random access, you'd have to iterate over the
entire range again to get to that element. Iterators are _far_ more efficient
than using indices. If you were to ask a linked list for its nth element, you'd
have to iterate over the entire list up to that element to get to it, meaning
that something like

for(size_t i = 0; i < list.size(); ++i)
    auto e = list.getIndex(i);

would actually end up iterating over the entire list multiple times in order to
get to each element. Contrast that with iterators, which iterate only once

for(list::iterator iter; iter < list.end(); ++iter)
    auto e = *iter;

Ranges are just a further evolution of iterators, and the result is
surprisingly flexible and powerful, especially when you start chaining function
calls. One of the major rationales behind ranges is discussed here:

http://www.drdobbs.com/architecture-and-design/component-programming-in-d/240008321

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list