On Wednesday, 9 December 2015 at 13:23:00 UTC, Tim K. wrote:
On Wednesday, 9 December 2015 at 13:13:36 UTC, BBaz wrote:

Is there a convenience function that allows me to remove an/all object(s) with a certain value from an array or do I need to write one myself?

Here are some elements of strategy:

     import std.algorithm;
     import std.array;

     auto arr = [2, 3, 4, 5, 3, 2, 4];

     // Remove all
     arr = arr.remove!(x => x==3);
     assert(arr == [2, 4, 5, 2, 4];);

// remove all (filter works lazilly, we use array to get an array nonetheless) // filter is useful when composing functions, not so much here
     arr = arr.filter!(x => x==2).array;
     assert(arr == [4, 5, 4]);

     // Remove one, throw if the element is not found,
     // we use countUntil to get the index.
     arr = arr.remove(arr.countUntil(4));
     assert(arr == [5, 4]);

also you may be interested in reading this:

http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays

Reply via email to