Thread overview | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 20, 2014 pop & popFront combined | ||||
---|---|---|---|---|
| ||||
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? |
September 20, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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?
Sometimes after popping, the previous `front` is no longer valid, such as in the case of a buffer being reused. We should be careful about promoting using a previously read `front` after `popFront` until we figure out what we want to do about these "transient ranges".
If you want move semantics, use `moveFront`.
|
September 20, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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? |
September 20, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jakob Ovrum | On Saturday, 20 September 2014 at 19:23:46 UTC, Jakob Ovrum wrote:
> Sometimes after popping, the previous `front` is no longer valid, such as in the case of a buffer being reused. We should be careful about promoting using a previously read `front` after `popFront` until we figure out what we want to do about these "transient ranges".
>
> If you want move semantics, use `moveFront`.
Excellent! Thanks!
|
September 20, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On 09/20/2014 11:59 AM, "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? It is also related to exception safety. It took the C++ community to realize that a Stack data structure with pop() returning the top object cannot be made exception safe. The solution was to separate pop() from top(). Here is the original paper demonstrating the impossibility: http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/aw/meyerscddemo/DEMO/MAGAZINE/CA_FRAME.HTM Here is Herb Sutter's presentation of the solution: http://www.gotw.ca/gotw/008.htm Ali |
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jakob Ovrum | On Saturday, 20 September 2014 at 19:23:46 UTC, Jakob Ovrum wrote:
> If you want move semantics, use `moveFront`.
But x.moveFront doesn't modify x.
What I want is to transform my uses of std.range from
if (!x.empty)
{
x.front.doStuff;
x.popFront;
}
into
if (!x.empty)
if (auto front = x.stealFront)
{
front.doStuff;
}
This is more functional/atomic, that is it reduces the risk of accidentally forgetting to call popFront at the end.
Destroy!
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 1 November 2014 at 11:43:28 UTC, Nordlöw wrote:
> if (!x.empty)
> if (auto front = x.stealFront)
> {
> front.doStuff;
> }
>
> This is more functional/atomic, that is it reduces the risk of accidentally forgetting to call popFront at the end.
Forgot my explicit question:
So why isn't something like x.stealFront already in Phobos?
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | On Saturday, 20 September 2014 at 19:40:02 UTC, AsmMan wrote:
> Is this function part of phobos library? if so, where?
It's in std.range.
|
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 1 November 2014 at 11:45:25 UTC, Nordlöw wrote: > So why isn't something like x.stealFront already in Phobos? First try here: https://github.com/nordlow/justd/blob/master/range_ex.d#L14 Please comment! |
November 01, 2014 Re: pop & popFront combined | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Saturday, 1 November 2014 at 13:22:34 UTC, Nordlöw wrote:
> https://github.com/nordlow/justd/blob/master/range_ex.d#L14
>
> Please comment!
What's the recommended way of making stealFront and stealBack inout here? Can I somehow use auto ref together with inout?
|
Copyright © 1999-2021 by the D Language Foundation