[Issue 2976] New: rename retreatN to retreat

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 13 13:25:27 PDT 2009


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

           Summary: rename retreatN to retreat
           Product: D
           Version: 2.030
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: ddoc, patch
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: k-foley at onu.edu


I think it was named retreatN before because the name retreat was already
taken.  Now that retreat is free, I think retreatN should be renamed to retreat
to complement advance (it's not advanceN).

As a bonus, I've fixed documentation misprints:

/**
Eagerly retreats $(D r) itself (not a copy) $(D n) times (by calling
$(D r.popBack) $(D n) times). The pass of $(D r) into $(D
retreat) is by reference, so the original range is
affected. Completes in $(BIGOH 1) steps for ranges that support
slicing, and in $(BIGOH n) time for all other ranges.

Example:
----
int[] a = [ 1, 2, 3, 4, 5 ];
a.retreat(2);
assert(a == [ 1, 2, 3 ]);
----
 */
size_t retreat(Range)(ref Range r, size_t n) if (isBidirectional!(Range))
{
    static if (hasSlicing!(Range) && hasLength!(Range))
    {
        auto newLen = n < r.length ? r.length - n : 0;
        n = r.length - newLen;
        r = r[0 .. newLen];
    }
    else
    {
        foreach (i; 0 .. n)
        {
            if (r.empty) return i;
            r.popBack;
        }
    }
    return n;
}

version(none) unittest
{
    int[] a = [ 1, 2, 3, 4, 5 ];
    a.retreat(2);
    assert(a == [ 1, 2, 3 ]);
}

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


More information about the Digitalmars-d-bugs mailing list