digitalmars.D.learn - We have slices, do we have Strides?
- Kai Meyer (11/11) Aug 08 2011 I have a problem I'd really like to use Strides for to simplify my code....
- Jonathan M Davis (2/16) Aug 08 2011 Would std.range.stride work for you?
- Kai Meyer (8/24) Aug 08 2011 It would, if there was a way to give it an offset:
- Jonathan M Davis (13/42) Aug 08 2011 The simplest way is to just pop off the appropriate number of elements f...
- Steven Schveighoffer (3/29) Aug 08 2011 -Steve
- Jonathan M Davis (3/34) Aug 08 2011 LOL. Yes. Since he's dealing with arrays rather than a generic input ran...
- Kai Meyer (2/33) Aug 09 2011 Awesome :) Thanks Steve!
- Kai Meyer (17/48) Aug 09 2011 Doh, how do I extract the array from the return value of stride?
- Steven Schveighoffer (22/74) Aug 09 2011 Two options, first, you can change do_something to accept a range
- Kai Meyer (2/80) Aug 09 2011 Both of those solutions worked, thanks :)
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
Aug 08 2011
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 MeyerWould std.range.stride work for you? - Jonathan M Davis
Aug 08 2011
On 08/08/2011 12:55 PM, Jonathan M Davis wrote: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?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 MeyerWould std.range.stride work for you? - Jonathan M Davis
Aug 08 2011
On 08/08/2011 12:55 PM, Jonathan M Davis wrote: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 DavisIt 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?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 MeyerWould std.range.stride work for you? - Jonathan M Davis
Aug 08 2011
On Mon, 08 Aug 2011 18:33:55 -0400, Kai Meyer <kai unixlords.com> wrote:On 08/08/2011 12:55 PM, Jonathan M Davis wrote:fixed: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 ];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 MeyerWould std.range.stride work for you? - Jonathan M Davisassert(equal(stride(a, 3), [ 1, 4, 7, 10 ][])); assert(equal(stride(a[1..$], 3), [ 2, 5, 8, 11 ][])); assert(equal(stride(a[2..$], 3), [ 3, 6, 9 ][])); assert(equal(stride(a[3..$], 3), [ 4, 7, 10 ][]));-Steve
Aug 08 2011
On Mon, 08 Aug 2011 18:33:55 -0400, Kai Meyer <kai unixlords.com> wrote:LOL. Yes. Since he's dealing with arrays rather than a generic input range, that works great. - Jonathan M DavisOn 08/08/2011 12:55 PM, Jonathan M Davis wrote:fixed: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 ];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 MeyerWould std.range.stride work for you? - Jonathan M Davisassert(equal(stride(a, 3), [ 1, 4, 7, 10 ][])); assert(equal(stride(a[1..$], 3), [ 2, 5, 8, 11 ][])); assert(equal(stride(a[2..$], 3), [ 3, 6, 9 ][])); assert(equal(stride(a[3..$], 3), [ 4, 7, 10 ][]));
Aug 08 2011
On 08/08/2011 05:25 PM, Steven Schveighoffer wrote:On Mon, 08 Aug 2011 18:33:55 -0400, Kai Meyer <kai unixlords.com> wrote:Awesome :) Thanks Steve!On 08/08/2011 12:55 PM, Jonathan M Davis wrote:fixed: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 ];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 MeyerWould std.range.stride work for you? - Jonathan M Davisassert(equal(stride(a, 3), [ 1, 4, 7, 10 ][])); assert(equal(stride(a[1..$], 3), [ 2, 5, 8, 11 ][])); assert(equal(stride(a[2..$], 3), [ 3, 6, 9 ][])); assert(equal(stride(a[3..$], 3), [ 4, 7, 10 ][]));-Steve
Aug 09 2011
On 08/08/2011 05:25 PM, Steven Schveighoffer wrote:On Mon, 08 Aug 2011 18:33:55 -0400, Kai Meyer <kai unixlords.com> wrote:Doh, how do I extract the array from the return value of stride? import std.range; int do_something(int[] values) { return values[0]; } void main(string[] args) { int[] a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]; do_something(stride(a,3)); } I get: moo.d(9): Error: function moo.do_something (int[] values) is not callable using argument types (Result) moo.d(9): Error: cannot implicitly convert expression (stride(a,3LU)) of type Result to int[]On 08/08/2011 12:55 PM, Jonathan M Davis wrote:fixed: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 ];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 MeyerWould std.range.stride work for you? - Jonathan M Davisassert(equal(stride(a, 3), [ 1, 4, 7, 10 ][])); assert(equal(stride(a[1..$], 3), [ 2, 5, 8, 11 ][])); assert(equal(stride(a[2..$], 3), [ 3, 6, 9 ][])); assert(equal(stride(a[3..$], 3), [ 4, 7, 10 ][]));-Steve
Aug 09 2011
On Tue, 09 Aug 2011 11:29:52 -0400, Kai Meyer <kai unixlords.com> wrote:On 08/08/2011 05:25 PM, Steven Schveighoffer wrote:Two options, first, you can change do_something to accept a range (depending on what the real implementation of do_something is, you might need to require a random-access range, but that dummy implementation could accept any input range, just return x.front): int do_something(R)(R values) if (isInputRange!R && is(ElementType!R : int)) { return values.front; // maybe need random access in real code? } Second option, convert range to an array. This incurs a memory allocation to create the array: import std.array; ... void main(...) { ... do_something(array(stride(a, 3))); } doc for std.array.array: http://www.d-programming-language.org/phobos/std_array.html#array -SteveOn Mon, 08 Aug 2011 18:33:55 -0400, Kai Meyer <kai unixlords.com> wrote:Doh, how do I extract the array from the return value of stride? import std.range; int do_something(int[] values) { return values[0]; } void main(string[] args) { int[] a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]; do_something(stride(a,3)); } I get: moo.d(9): Error: function moo.do_something (int[] values) is not callable using argument types (Result) moo.d(9): Error: cannot implicitly convert expression (stride(a,3LU)) of type Result to int[]On 08/08/2011 12:55 PM, Jonathan M Davis wrote:fixed: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 ];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 MeyerWould std.range.stride work for you? - Jonathan M Davisassert(equal(stride(a, 3), [ 1, 4, 7, 10 ][])); assert(equal(stride(a[1..$], 3), [ 2, 5, 8, 11 ][])); assert(equal(stride(a[2..$], 3), [ 3, 6, 9 ][])); assert(equal(stride(a[3..$], 3), [ 4, 7, 10 ][]));-Steve
Aug 09 2011
On 08/09/2011 09:37 AM, Steven Schveighoffer wrote:On Tue, 09 Aug 2011 11:29:52 -0400, Kai Meyer <kai unixlords.com> wrote:Both of those solutions worked, thanks :)On 08/08/2011 05:25 PM, Steven Schveighoffer wrote:Two options, first, you can change do_something to accept a range (depending on what the real implementation of do_something is, you might need to require a random-access range, but that dummy implementation could accept any input range, just return x.front): int do_something(R)(R values) if (isInputRange!R && is(ElementType!R : int)) { return values.front; // maybe need random access in real code? } Second option, convert range to an array. This incurs a memory allocation to create the array: import std.array; ... void main(...) { ... do_something(array(stride(a, 3))); } doc for std.array.array: http://www.d-programming-language.org/phobos/std_array.html#array -SteveOn Mon, 08 Aug 2011 18:33:55 -0400, Kai Meyer <kai unixlords.com> wrote:Doh, how do I extract the array from the return value of stride? import std.range; int do_something(int[] values) { return values[0]; } void main(string[] args) { int[] a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]; do_something(stride(a,3)); } I get: moo.d(9): Error: function moo.do_something (int[] values) is not callable using argument types (Result) moo.d(9): Error: cannot implicitly convert expression (stride(a,3LU)) of type Result to int[]On 08/08/2011 12:55 PM, Jonathan M Davis wrote:fixed: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 ];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 MeyerWould std.range.stride work for you? - Jonathan M Davisassert(equal(stride(a, 3), [ 1, 4, 7, 10 ][])); assert(equal(stride(a[1..$], 3), [ 2, 5, 8, 11 ][])); assert(equal(stride(a[2..$], 3), [ 3, 6, 9 ][])); assert(equal(stride(a[3..$], 3), [ 4, 7, 10 ][]));-Steve
Aug 09 2011