pop & popFront combined

2014-09-20 Thread Nordlöw
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?

Re: pop & popFront combined

2016-05-11 Thread Nordlöw via Digitalmars-d-learn
On Saturday, 1 November 2014 at 13:22:34 UTC, Nordlöw wrote: https://github.com/nordlow/justd/blob/master/range_ex.d#L14 Moved here: https://github.com/nordlow/phobos-next/blob/master/src/range_ex.d#L66

Re: pop & popFront combined

2014-09-20 Thread Jakob Ovrum via Digitalmars-d-learn
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 poppi

Re: pop & popFront combined

2014-09-20 Thread AsmMan via Digitalmars-d-learn
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 c

Re: pop & popFront combined

2014-09-20 Thread Nordlöw
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 wa

Re: pop & popFront combined

2014-09-20 Thread Ali Çehreli via Digitalmars-d-learn
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 too

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
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 fron

Re: pop & popFront combined

2014-11-01 Thread 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

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
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.

Re: pop & popFront combined

2014-11-01 Thread 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!

Re: pop & popFront combined

2014-11-01 Thread 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?

Re: pop & popFront combined

2014-11-01 Thread anonymous via Digitalmars-d-learn
On Saturday, 1 November 2014 at 13:22:34 UTC, Nordlöw wrote: 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! That is: auto

Re: pop & popFront combined

2014-11-01 Thread anonymous via Digitalmars-d-learn
On Saturday, 1 November 2014 at 13:25:03 UTC, Nordlöw wrote: 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 au

Re: pop & popFront combined

2014-11-01 Thread via Digitalmars-d-learn
On Saturday, 1 November 2014 at 13:30:16 UTC, anonymous wrote: On Saturday, 1 November 2014 at 13:22:34 UTC, Nordlöw wrote: 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/just

Re: pop & popFront combined

2014-11-01 Thread anonymous via Digitalmars-d-learn
On Saturday, 1 November 2014 at 13:36:05 UTC, Marc Schütz wrote: On Saturday, 1 November 2014 at 13:30:16 UTC, anonymous wrote: [...] auto ref stealFront(R)(ref R r) { import std.range: moveFront, popFront; auto e = r.moveFront; r.popFront; return e; } [...] It's probably intended to mean `aut

Re: pop & popFront combined

2014-11-01 Thread via Digitalmars-d-learn
On Saturday, 1 November 2014 at 13:25:03 UTC, Nordlöw wrote: 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 au

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
On Saturday, 1 November 2014 at 13:38:22 UTC, Marc Schütz wrote: If you want to avoid the temporary variable, you could write: scope(success) r.popFront; return r.moveFront; Does this solution cost performance?

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
On Saturday, 1 November 2014 at 13:30:16 UTC, anonymous wrote: `auto ref` is nonsense here. You can't return a reference to `e` as it's a local variable. My mistake. Thanks.

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
On Saturday, 1 November 2014 at 13:36:05 UTC, anonymous wrote: I don't see what you'd need inout for here. stealFront/stealBack are not methods. inout can be used on free functions aswell. See for example https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L674

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
On Saturday, 1 November 2014 at 13:38:22 UTC, Marc Schütz wrote: If you want to avoid the temporary variable, you could write: scope(success) r.popFront; return r.moveFront; Nice solution anyhow! Thanks!

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
On Saturday, 1 November 2014 at 13:54:31 UTC, Nordlöw wrote: On Saturday, 1 November 2014 at 13:38:22 UTC, Marc Schütz wrote: If you want to avoid the temporary variable, you could write: scope(success) r.popFront; return r.moveFront; Does this solution cost performance? I guess we ha

Re: pop & popFront combined

2014-11-01 Thread anonymous via Digitalmars-d-learn
On Saturday, 1 November 2014 at 14:01:42 UTC, Nordlöw wrote: On Saturday, 1 November 2014 at 13:36:05 UTC, anonymous wrote: I don't see what you'd need inout for here. stealFront/stealBack are not methods. inout can be used on free functions aswell. See for example https://github.com/nordlow/

Re: pop & popFront combined

2014-11-01 Thread Jakob Ovrum via Digitalmars-d-learn
On Saturday, 1 November 2014 at 11:43:28 UTC, Nordlöw wrote: 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. It does modify `x` as it leaves `front` in a destroyed and default-initialized state

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
On Saturday, 1 November 2014 at 16:10:17 UTC, Jakob Ovrum wrote: The other half of my post explained why such a `stealFront` is problematic. Got it. Thanks!

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
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 This seems like a error-prone design to me. I guess performance is the motivation right? Maybe a future

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
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. Is there a suitable trait we can use to detect this and in turn use to disallow stealFront() and stealBack() in the

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
On Saturday, 1 November 2014 at 16:10:17 UTC, Jakob Ovrum wrote: problematic. What about turning stealFront and stealBack at https://github.com/nordlow/justd/blob/master/range_ex.d into mixins?

Re: pop & popFront combined

2014-11-01 Thread Nordlöw
On Saturday, 1 November 2014 at 20:48:45 UTC, Nordlöw wrote: Is there a suitable trait we can use to detect this and in turn use to disallow stealFront() and stealBack() in these cases? Made it a separate new question at http://forum.dlang.org/thread/onibkzepudfisxtri...@forum.dlang.org#post-o

Re: pop & popFront combined

2014-11-02 Thread via Digitalmars-d-learn
On Saturday, 1 November 2014 at 14:19:56 UTC, Nordlöw wrote: On Saturday, 1 November 2014 at 13:54:31 UTC, Nordlöw wrote: On Saturday, 1 November 2014 at 13:38:22 UTC, Marc Schütz wrote: If you want to avoid the temporary variable, you could write: scope(success) r.popFront; return r.moveF

Re: pop & popFront combined

2014-11-02 Thread via Digitalmars-d-learn
On Saturday, 1 November 2014 at 13:54:31 UTC, Nordlöw wrote: On Saturday, 1 November 2014 at 13:38:22 UTC, Marc Schütz wrote: If you want to avoid the temporary variable, you could write: scope(success) r.popFront; return r.moveFront; Does this solution cost performance? I think DMD d

Re: pop & popFront combined

2014-11-02 Thread Nordlöw
On Sunday, 2 November 2014 at 11:46:19 UTC, Marc Schütz wrote: I think DMD doesn't generate good code for it; IIRC it lowers scope(success) to a strange construct with an invisible variable and a try/catch. Don't know the reasons for this, maybe it has changed by now. Theoretically it would jus

Re: pop & popFront combined

2014-11-02 Thread via Digitalmars-d-learn
On Sunday, 2 November 2014 at 12:29:14 UTC, Nordlöw wrote: On Sunday, 2 November 2014 at 11:46:19 UTC, Marc Schütz wrote: I think DMD doesn't generate good code for it; IIRC it lowers scope(success) to a strange construct with an invisible variable and a try/catch. Don't know the reasons for th