www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10754] New: std.range.rotate?

http://d.puremagic.com/issues/show_bug.cgi?id=10754

           Summary: std.range.rotate?
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-08-03 13:09:02 PDT ---
std.algorithm.bringToFront is efficient to rotate arrays in-place, but often in
range-based coding (in UFCS chains) I'd like a lazy range that yields the
rotated items and doesn't modify the order of the items of the original data.
So in Phobos I'd like a lazy range with a semantics similar to this:

import std.stdio, std.range;

auto rotate(R)(R r, in int n) pure nothrow
if (isRandomAccessRange!R) {
    immutable int len = r.walkLength;
    return r.cycle.drop(n >= 0 ? n : len + n).take(len);
}

void main() {
    foreach (shift; -5 .. 5)
        [10, 20, 30, 40, 50].rotate(shift).writeln;
}


To work like this the input array should be a random access one. (You can write
a rotate() for a bidirectional array, but I think there is less need for it).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 03 2013