C++ / Why Iterators Got It All Wrong

Robert M. Münch via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 30 23:50:15 PDT 2017


On 2017-08-29 13:23:50 +0000, Steven Schveighoffer said:

> ...
> In Phobos, find gives you a range where the first element is the one 
> you searched for, and the last element is the end of the original 
> range. But what if you wanted all the data *up to* the element instead? 
> What if you just wanted to look at that specific element? So we need 
> several functions that do this, and it's not always clear how to do it 
> correctly, and it's difficult to compose one function from the other.

I'm a big fan of Rebol (yes, it's far from mainstream and a dead-end 
these days but it has a lot of very nice ideas) and it uses the idea of 
a series datatype. Fruther information here: 
http://www.rebol.com/docs/core23/rebolcore-6.html

There you do something like:

1. Return the first hit and the rest of the series

>> my-series: "abcdefg"
== "abcdefg"
>> find my-series "b"
== "bcdefg"


2. Return only the hit

>> first find my-series "b"
== #"b"


3. Return everything before the first hit

>> copy/part my-series find my-series "d"
== "abc"


If you ever got used to such a thinking, writing & using more 
non-functional approaches, really hurts.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d mailing list