std.algorithm.remove from array of custom classes?
cym13 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Dec 9 13:01:16 PST 2015
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
More information about the Digitalmars-d-learn
mailing list