Removing an object from a range
Steven Schveighoffer
schveiguy at yahoo.com
Mon Dec 13 06:54:35 PST 2010
On Sun, 12 Dec 2010 20:43:12 -0500, Andrej M. <none at none.com> wrote:
> I can't seem to find an easy remove method in std.algorithm that takes
> an object and a range (an array in this case) and removes any matches
> from the range. I'm using this snippet for now:
>
> private DrawingElement[] elements;
>
> public override void Remove(DrawingElement d)
> {
> foreach (i, child; elements)
> {
> if (child == d)
> {
> elements = remove(elements, i);
> break;
> }
> }
> }
>
> Ugly! :)
>
> It's a direct port from some C# code so don't mind the silly variable
> names.
does this work?
elementsRemoved = std.algorithm.remove!((child){return child ==
d})(elements);
From there, you can shrink the original array like:
elements = elements[0..$-elementsRemoved.length];
Search for remove on
http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html (there are
several cases, look for the one that takes a predicate)
-Steve
More information about the Digitalmars-d-learn
mailing list