digitalmars.D.learn - simple range question
- Laeeth Isharc (5/5) Apr 08 2016 suppose I have a forward or random access range. what's the best
- Jonathan M Davis via Digitalmars-d-learn (14/19) Apr 08 2016 Well, if you have a random access range, you can just use indices and lo...
- jmh530 (13/18) Apr 08 2016 I would recommend writing a good lag function. If we have a good
- Edwin van Leeuwen (4/9) Apr 08 2016 I'd do it like this, but I guess that is what you mean with
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 ?
Apr 08 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
Apr 08 2016
On Friday, 8 April 2016 at 18:27:59 UTC, Laeeth Isharc 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 ?I would recommend writing a good lag function. If we have a good lag function, then you could easily do something like x.zip(x.lag(lookback)).map!(a => a[0] > a[1]); I could think of a few different ways to implement a lag function, but it would take some time to write a good one (including NA padding, for instance). You could probably use popBackN or popFrontN/drop to accomplish it, depending on forward or backward lags. Alternately, you could probably figure something out with .save that avoids the use of a lag function. You could create a range that saves up to four values. When full you pop off the front, make your comparison, and then save the latest.
Apr 08 2016
On Friday, 8 April 2016 at 18:27:59 UTC, Laeeth Isharc 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 ?I'd do it like this, but I guess that is what you mean with mapping it to a tuple: zip( r, r[4..$] ).map!((t) => t[0] == t[1]);
Apr 08 2016