Algorithm remove Tid
import std.algorithm; import std.concurrency; void main() { Tid[] tids = []; Tid tid = thisTid; tids ~= tid; tids.remove(tid); } Why does this not compile?
Re: Algorithm remove Tid
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)();
Re: Algorithm remove Tid
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
Re: Algorithm remove Tid
Casper Færgemand: To remove an element, I *think* you do it this way: tids = tids.remove!(a=a == tid)(); is that removing only 0 or 1 items? Bye, bearophile
Re: Algorithm remove Tid
On Wednesday, 22 January 2014 at 13:51:51 UTC, bearophile wrote: Casper Færgemand: To remove an element, I *think* you do it this way: tids = tids.remove!(a=a == tid)(); is that removing only 0 or 1 items? Bye, bearophile It removes all items that match the tid.
Re: Algorithm remove Tid
On Wednesday, 22 January 2014 at 13:51:51 UTC, bearophile wrote: Casper Færgemand: To remove an element, I *think* you do it this way: tids = tids.remove!(a=a == tid)(); is that removing only 0 or 1 items? Bye, bearophile Maybe you confusing the new style lambda for a greater equal operator? I can't make sense of your question any other way.
Re: Algorithm remove Tid
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
Re: Algorithm remove Tid
On Wednesday, 22 January 2014 at 15:41:58 UTC, bearophile wrote: 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 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.
Re: Algorithm remove Tid
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
Re: Algorithm remove Tid
On Wednesday, 22 January 2014 at 16:48:45 UTC, bearophile wrote: monarch_dodra: There's no phobos solution for that, There will be. In the meantime use: items = items.remove(items.countUntil(needle)); 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. See also: https://d.puremagic.com/issues/show_bug.cgi?id=10959 Thx. Bye, bearophile