Bug 2976 – rename retreatN to retreat

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-05-13T13:25:00Z
Last change time
2015-06-09T01:27:57Z
Keywords
ddoc, patch
Assigned to
andrei
Creator
k-foley

Comments

Comment #0 by k-foley — 2009-05-13T13:25:26Z
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 ]); }
Comment #1 by k-foley — 2009-05-13T13:37:08Z
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.
Comment #2 by andrei — 2009-08-27T14:39:32Z
I ended up calling it popBackN.
Comment #3 by k-foley — 2009-08-27T17:59:48Z
Are you going to rename advance to popFrontN?
Comment #4 by andrei — 2009-08-27T18:23:40Z
(In reply to comment #3) > Are you going to rename advance to popFrontN? Great idea! Just did so, thanks.