> On 08/08/2011 12:55 PM, Jonathan M Davis wrote: > >> I have a problem I'd really like to use Strides for to simplify my code. > >> Currently, I do this: > >> foreach(n; 0..chunks) > >> comp_arr[n] = values[(n * step_size) + n] > >> if(!all_same(comp_arr, comp_arr[0])) > >> > >> It would eliminate an entire 2 lines of code for each time I want > >> strides, to be able to do this: > >> if(!all_same(bytes[i..$..step_size]) > >> > >> Meaning, start with i, grab all elements at i + block_size * n until > >> block_size * n> bytes.length. Right? > >> > >> -Kai Meyer > > > > Would std.range.stride work for you? > > > > - Jonathan M Davis > > It would, if there was a way to give it an offset: > > int[] a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]; > assert(equal(stride(a, 3), [ 1, 4, 7, 10 ][])); > assert(equal(stride(a, 3, 1), [ 2, 5, 8, 11 ][])); > assert(equal(stride(a, 3, 2), [ 3, 6, 9 ][])); > assert(equal(stride(a, 3, 3), [ 4, 7, 10 ][])); > > Is that possible?
The simplest way is to just pop off the appropriate number of elements from the front. Now, maybe stride should be enhanced to take an offset, but you can do it by just popping off the appropriate number of elements first. Another option would be drop, if it ends up in the next release: https://github.com/D-Programming-Language/phobos/pull/147 It would allow you to drop pop the elements from a slice of a which would then be passed to stride instead of having to create a slice and pop off the elements before calling stride. If it gets into Phobos though, that probably makes it so that there's no real need to make stride take an offset. Regardless, for the moment, it looks like you need to pop off the appropriate number of elements from a (or a slice of a) before passing it to stride if you want an offset. - Jonathan M Davis
