pop & popFront combined

AsmMan via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 20 12:40:01 PDT 2014


On Saturday, 20 September 2014 at 18:59:03 UTC, Nordlöw wrote:
> Is there a reason why popFront doesn't automatically return 
> what front does?
>
> If so I'm still missing a combined variant of pop and popFront 
> in std.range.
> Why isn't such a common operation in Phobos already?

So far I know isn't common use return value from popFront() at 
same time it's called. For example, checkout how is:

int[] a = [1,2,3];
foreach(int n; a) {}

is translated:

for(auto r = a; !r.empty; r.popFront())
{
   int n = r.front;
}

to return same as value in front by popFront() save previously 
value is needed:

int popFrontInt(out int[] arr)
{
    int current = arr[0]; // or arr.front
    arr = arr[1 .. $];
    return current;
}

(this isn't exactly like Phobos implementation and is int[]-only, 
btw)

the cost of the 'current' variable may be a bit expansive (one 
extra register use per function call) and useless, since it isn't 
used and a common use is one like the loop.

I think it's well-designed, IMHO...

> On Saturday, 20 September 2014 at 18:59:03 UTC, Nordlöw wrote:
> If you want move semantics, use `moveFront`.

Is this function part of phobos library? if so, where?


More information about the Digitalmars-d-learn mailing list