rotate left an array

Paul Backus snarwin at gmail.com
Tue Oct 4 00:00:31 UTC 2022


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;
>     range.popFront();
>     return chain(range, only(fr));
> }

Tiny nitpick: this should use

     const fr = range.save.front;

...to ensure that `fr` is not invaliated or overwritten by the 
subsequent call to range.popFront (e.g., think of File.byLine 
here).


More information about the Digitalmars-d-learn mailing list