www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2976] New: rename retreatN to retreat

reply d-bugmail puremagic.com writes:
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 digitalmars.com
        ReportedBy: k-foley 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: -------
May 13 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2976






I forgot to comment on changing the concept for the function.  I changed from:
----
size_t retreat(Range)(ref Range r, size_t n) if (isInputRange!(Range))
{
 ...
}
----
to:
----
size_t retreat(Range)(ref Range r, size_t n) if (isBidirectionalRange!(Range))
{
 ...
}

I know the first is not correct, but I am unsure about isBidirectionalRange,
since retreat really only requires popBack and empty.  It looks like the best
match without inventing isRetroInputRange or similar.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 13 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2976


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED





14:39:32 PDT ---
I ended up calling it popBackN.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 27 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2976






Are you going to rename advance to popFrontN?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 27 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2976






18:23:40 PDT ---

 Are you going to rename advance to popFrontN?
Great idea! Just did so, thanks. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 27 2009