digitalmars.D.learn - Algorithm remove Tid
- "Casper =?UTF-8?B?RsOmcmdlbWFuZCI=?= <shorttail hotmail.com> (9/9) Jan 22 2014 import std.algorithm;
- monarch_dodra (5/14) Jan 22 2014 Because "remove" takes an "offset" as an argument, not an element.
- "Casper =?UTF-8?B?RsOmcmdlbWFuZCI=?= <shorttail hotmail.com> (4/8) Jan 22 2014 Thanks a lot. I was trying to get that part to work, but I had a
- bearophile (4/7) Jan 22 2014 is that removing only 0 or 1 items?
- "Casper =?UTF-8?B?RsOmcmdlbWFuZCI=?= <shorttail hotmail.com> (2/9) Jan 22 2014 It removes all items that match the tid.
- monarch_dodra (3/10) Jan 22 2014 Maybe you confusing the new style lambda for a "greater equal"
- bearophile (6/8) Jan 22 2014 My point was that the shown code doesn't remove only one item in
- monarch_dodra (33/42) Jan 22 2014 Ah... I see. Yeah, this will remove *all* items that match the
- bearophile (8/9) Jan 22 2014 There will be.
- monarch_dodra (6/15) Jan 22 2014 Hum... that requires iterating the range twice for a non-RA
import std.algorithm; import std.concurrency; void main() { Tid[] tids = []; Tid tid = thisTid; tids ~= tid; tids.remove(tid); } Why does this not compile?
Jan 22 2014
On Wednesday, 22 January 2014 at 12:11:22 UTC, Casper Færgemand wrote:import std.algorithm; import std.concurrency; void main() { Tid[] tids = []; Tid tid = thisTid; tids ~= tid; tids.remove(tid); } Why does this not compile?Because "remove" takes an "offset" as an argument, not an element. To remove an element, I *think* you do it this way: tids = tids.remove!(a=>a == tid)();
Jan 22 2014
On Wednesday, 22 January 2014 at 13:16:18 UTC, monarch_dodra wrote:Because "remove" takes an "offset" as an argument, not an element. To remove an element, I *think* you do it this way: tids = tids.remove!(a=>a == tid)();Thanks a lot. I was trying to get that part to work, but I had a hard time realizing that => was a lambda and not a >=. x.x
Jan 22 2014
Casper Færgemand:is that removing only 0 or 1 items? Bye, bearophileTo remove an element, I *think* you do it this way: tids = tids.remove!(a=>a == tid)();
Jan 22 2014
On Wednesday, 22 January 2014 at 13:51:51 UTC, bearophile wrote:Casper Færgemand:It removes all items that match the tid.is that removing only 0 or 1 items? Bye, bearophileTo remove an element, I *think* you do it this way: tids = tids.remove!(a=>a == tid)();
Jan 22 2014
On Wednesday, 22 January 2014 at 13:51:51 UTC, bearophile wrote:Casper Færgemand:Maybe you confusing the new style lambda for a "greater equal" operator? I can't make sense of your question any other way.is that removing only 0 or 1 items? Bye, bearophileTo remove an element, I *think* you do it this way: tids = tids.remove!(a=>a == tid)();
Jan 22 2014
monarch_dodra:Maybe you confusing the new style lambda for a "greater equal" operator? I can't make sense of your question any other way.My point was that the shown code doesn't remove only one item in presence of duplicated ones. In this case tid are unique, but in general using that code to remove one item is not a good idea. Bye, bearophile
Jan 22 2014
On Wednesday, 22 January 2014 at 15:41:58 UTC, bearophile wrote:monarch_dodra:Ah... I see. Yeah, this will remove *all* items that match the TID. I'm not sure that's a problem in this context, but I you did want to remove "at most" 1 item, then this isn't the correct solution. There's no phobos solution for that, but I guess it would be written something like: template removeOne(alias pred, SwapStrategy s = SwapStrategy.stable) { Range removeOne(Range)(Range range) { auto result = range.save; auto f = find!pred(range); if (f.empty) return result; static if (s == SwapStrategy.stable) { auto ff = f.save; f.popFront(); ff.popBack; for ( ; !f.empty; f.popFront(), ff.popFront()) moveFront(f, ff); } else { move(find.back, find.front); } result.popBack(); return result; } } Disclaimer: Not actually tested. May also horribly fail on non-reference ranges.Maybe you confusing the new style lambda for a "greater equal" operator? I can't make sense of your question any other way.My point was that the shown code doesn't remove only one item in presence of duplicated ones. In this case tid are unique, but in general using that code to remove one item is not a good idea. Bye, bearophile
Jan 22 2014
monarch_dodra:There's no phobos solution for that,There will be. In the meantime use: items = items.remove(items.countUntil(needle)); See also: https://d.puremagic.com/issues/show_bug.cgi?id=10959 Bye, bearophile
Jan 22 2014
On Wednesday, 22 January 2014 at 16:48:45 UTC, bearophile wrote:monarch_dodra:Hum... that requires iterating the range twice for a non-RA range. And you forgot a save: items = items.remove(items.save.countUntil(needle)); But it *is* much simpler.There's no phobos solution for that,There will be. In the meantime use: items = items.remove(items.countUntil(needle));See also: https://d.puremagic.com/issues/show_bug.cgi?id=10959Thx.Bye, bearophile
Jan 22 2014