rotate left an array

Ali Çehreli acehreli at yahoo.com
Mon Oct 3 21:06:36 UTC 2022


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));
}

void main()
{
     import std.algorithm : equal;
     int[] arr1 = [ 1, 2, 3, 4 ];
     assert(arr1.rotatedView.equal([ 2, 3, 4, 1 ]));

     // Now this is getting a little expensive! :)
     assert(arr1
            .rotatedView
            .rotatedView
            .equal([ 3, 4, 1, 2 ]));
}

Ali




More information about the Digitalmars-d-learn mailing list