simple range question

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 8 12:03:12 PDT 2016


On Friday, April 08, 2016 18:27:59 Laeeth Isharc via Digitalmars-d-learn 
wrote:
> suppose I have a forward or random access range.  what's the best
> way to compare each element with the element 4 elements prior to
> that element?  I could map each element to a tuple of the element
> and the element 4 bars previously and do it that way.  any neater
> way ?

Well, if you have a random access range, you can just use indices and loop
over, making it so that one is getting i - 4 and the other is getting i for
its element. Or if you don't have random access, you could just pop 4
elements off the front of one and then use equal to compare the ranges (and
of course compare copies gotten via save if you don't want to actually
consume those elements). e.g. something like

auto result = range1.save.drop(4).equal(range2.save);

Though of course, the lengths of the ranges have to match up appropriately
for that. Slicing and then comparing with equal might be a good choice too.
But without having more specifics on what exactly you're doing, it's kind of
hard to give a much better suggestion.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list