Re: rotate left an array

2022-10-04 Thread Christian Köstlin via Digitalmars-d-learn
If you are ok with using things from std.range you could use something like this: ```d import std.range : cycle, drop, take; import std.stdio : writeln; int main(string[] args) { auto r = [1, 2, 3, 4, 5, 6, 7, 8]; writeln(r.cycle.drop(3).take(r.length)); return 0; } ``` Kind regar

Re: rotate left an array

2022-10-03 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Oct 03, 2022 at 05:38:25PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: [...] > Good catch but I think what we want is a copy of the front element, at > least for InputRanges (.save does not work for File.byLine :/). One of the things we need to settle in Phobos v2 is what to do with

Re: rotate left an array

2022-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 4 October 2022 at 00:38:25 UTC, Ali Çehreli wrote: Good catch but I think what we want is a copy of the front element, at least for InputRanges (.save does not work for File.byLine :/). What is the generic way of copying an element? I wonder whether we have to use isSomeString to

Re: rotate left an array

2022-10-03 Thread rassoc via Digitalmars-d-learn
On 10/3/22 23:06, Ali Çehreli via Digitalmars-d-learn wrote: auto rotatedView(R)(R range) Or even more generic by chaining two slices in case the range permits it: auto rotatedView(R)(R range, long n = 1) if (...) { if (n == 0) return range; ... n %= range.length;

Re: rotate left an array

2022-10-03 Thread Ali Çehreli via Digitalmars-d-learn
On 10/3/22 17:00, Paul Backus wrote: > On Monday, 3 October 2022 at 21:06:36 UTC, Ali Çehreli wrote: >> On 10/3/22 13:48, Andrey Zherikov wrote: >>> a "rotated view". >> >> Without indexes: >> >> import std.range : empty; >> >> auto rotatedView(R)(R range) >> in (!range.empty) >> { >> import s

Re: rotate left an array

2022-10-03 Thread Paul Backus via Digitalmars-d-learn
On Monday, 3 October 2022 at 21:06:36 UTC, Ali Çehreli wrote: On 10/3/22 13:48, Andrey Zherikov wrote: a "rotated view". Without indexes: import std.range : empty; auto rotatedView(R)(R range) in (!range.empty) { import std.range : chain, front, only, popFront; const fr = range.front

Re: rotate left an array

2022-10-03 Thread Ali Çehreli via Digitalmars-d-learn
On 10/3/22 13:48, Andrey Zherikov wrote: a "rotated view". Without indexes: import std.range : empty; auto rotatedView(R)(R range) in (!range.empty) { import std.range : chain, front, only, popFront; const fr = range.front; range.popFront(); return chain(range, only(fr)); } v

Re: rotate left an array

2022-10-03 Thread Andrey Zherikov via Digitalmars-d-learn
On Monday, 3 October 2022 at 18:09:05 UTC, Fausto wrote: Hello all, I am trying to rotate left an array. I found a very basic way, and I am not sure if there is something clever than this :) maybe using slices... the external for represents how many times you are rotating (in this case 2

Re: rotate left an array

2022-10-03 Thread Nick Treleaven via Digitalmars-d-learn
On Monday, 3 October 2022 at 18:09:05 UTC, Fausto wrote: Hello all, I am trying to rotate left an array. I found a very basic way, and I am not sure if there is something clever than this :) maybe using slices... Here we can't use slice assignment instead of the inner loop because

rotate left an array

2022-10-03 Thread Fausto via Digitalmars-d-learn
Hello all, I am trying to rotate left an array. I found a very basic way, and I am not sure if there is something clever than this :) maybe using slices... the external for represents how many times you are rotating (in this case 2). ```d void main() { import std.range; import