array idioms
Jonathan M Davis
jmdavisProg at gmx.com
Fri Feb 25 13:39:03 PST 2011
On Friday, February 25, 2011 12:51:53 Andrej Mitrovic wrote:
> I have asked for remove before, I got some responses here (in fact,
> Jonathan made an enhancement request for it):
>
> http://www.digitalmars.com/d/archives/digitalmars/D/learn/Removing_an_objec
> t_from_a_range_23212.html
>
> The topic was derailed, but essentially you can provide a predicate as
> a function literal to do the work:
>
> import std.stdio;
> import std.algorithm;
>
> void main()
> {
> int[] elements = [1, 2, 3, 2, 4];
> int needle = 2;
>
> elements = remove!((item) {return item == needle;} )(elements);
> assert(elements == [1, 3, 4]);
> }
Actually, you can probably just use replace and give it an empty range for the
third argument. Still, there should probably be a function in std.array which
removes a value from an array based on its value. A function that's essentially
identical to the version of replace which takes two ranges in addition to the
array but doesn't take the range to replace with would probably do the trick:
R1 replace(R1, R2)(R1 subject, R2 toRemove) if(...) {...}
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list