genetically modified slices - is it possible?

Jonathan M Davis jmdavisProg at gmx.com
Sat Nov 16 03:38:37 PST 2013


On Friday, November 15, 2013 22:21:03 Alexandr Druzhinin wrote:
> 15.11.2013 22:13, bearophile пишет:
> > Alexandr Druzhinin:
> > 
> > A simple solution is to keep two indexes, and use them to find the
> > slices when you need them.
> 
> I did it the first. But then I decided to make it more D-ish and
> stumbled upon the issue. Wasn't it wrong decision and would be better to
> stay with indices?

Well, if you want to get a slice which refers to the same thing as that slice 
but includes elements which are in what is being sliced but which aren't in 
that slice, you can only get the slice that you want be reslicing whatever the 
slices came from. So, that means that you have to have a range which refers to 
all of the elements that you want your target range to refer to and possibly 
more and then either slice that range to exactly the elements that you want or 
pop off elements until you get the range that you want. Doing that generally 
requires knowing how many elements to pop off or the indices to slice, which 
basically means that you have to keep track of indices.

In general, it doesn't work very well to increase a range so that it covers 
more elements. They're designed to be reduced, not increased, and so the more 
"D" way of doing things with ranges essentially goes against what you're 
trying to do, forcing you to come at the problem from a different angle. And in 
this case, it sounds like that probably means keeping track of indices.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list