www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Phobos function to remove all occurances from dynamic array?

reply Liam McGillivray <yoshi.pit.link.mario gmail.com> writes:
This is presumably such a common task that I'm surprised it isn't 
easy to find the answer by searching;

Is there a standard library function that removes all elements 
from a dynamic array that matches an input argument?

In `std.array` there's the `replace` function which is supposed 
to replace all occurrences that match an input with another. It 
seems to work as described on strings, but I get compiler errors 
when using it on other array types. I've tried using it to 
replace occurrences of a certain object in an array with `[]` in 
order to remove all occurrences, but it's not allowed.

Is there a Phobos function that does what I want? It would be 
crazy if there isn't.
Apr 30
next sibling parent reply Lance Bachmeier <no spam.net> writes:
On Wednesday, 1 May 2024 at 01:09:33 UTC, Liam McGillivray wrote:
 This is presumably such a common task that I'm surprised it 
 isn't easy to find the answer by searching;

 Is there a standard library function that removes all elements 
 from a dynamic array that matches an input argument?

 In `std.array` there's the `replace` function which is supposed 
 to replace all occurrences that match an input with another. It 
 seems to work as described on strings, but I get compiler 
 errors when using it on other array types. I've tried using it 
 to replace occurrences of a certain object in an array with 
 `[]` in order to remove all occurrences, but it's not allowed.

 Is there a Phobos function that does what I want? It would be 
 crazy if there isn't.
Does filter do what you need? https://dlang.org/phobos/std_algorithm_iteration.html#.filter
Apr 30
parent Liam McGillivray <yoshi.pit.link.mario gmail.com> writes:
On Wednesday, 1 May 2024 at 01:24:55 UTC, Lance Bachmeier wrote:
 Does filter do what you need?

 https://dlang.org/phobos/std_algorithm_iteration.html#.filter
It seems to do it with the following line: ``` allObjects = allObjects.filter!(element => element !is this).array; ``` So I've found a way to do it. It's still rather strange that it's so difficult to find the solution to such a common problem in the documentation.
Apr 30
prev sibling next sibling parent Nick Treleaven <nick geany.org> writes:
On Wednesday, 1 May 2024 at 01:09:33 UTC, Liam McGillivray wrote:
 I get compiler errors when using it on other array types. I've 
 tried using it to replace occurrences of a certain object in an 
 array with [] in order to remove all occurrences, but it's not 
 allowed.
Can you post a code example?
May 01
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Wednesday, 1 May 2024 at 01:09:33 UTC, Liam McGillivray wrote:
 This is presumably such a common task that I'm surprised it 
 isn't easy to find the answer by searching;

 Is there a standard library function that removes all elements 
 from a dynamic array that matches an input argument?

 In `std.array` there's the `replace` function which is supposed 
 to replace all occurrences that match an input with another. It 
 seems to work as described on strings, but I get compiler 
 errors when using it on other array types. I've tried using it 
 to replace occurrences of a certain object in an array with 
 `[]` in order to remove all occurrences, but it's not allowed.

 Is there a Phobos function that does what I want? It would be 
 crazy if there isn't.
`remove` https://dlang.org/phobos/std_algorithm_mutation.html#remove ```d arr = arr.remove!(v => shouldBeRemoved(v)); ``` Why the reassignment? Because `remove` removes elements *in place*, and does not change the range extents. It returns the portion of the range that contains the unremoved elements. So to give an example: ```d auto arr = [1, 2, 3, 4, 5]; auto result = arr.remove!(i => i % 2 == 1); // remove odd elements assert(result == [2, 4]); // first 2 are the slice that is stored in result // the last three are leftovers. assert(arr == [2, 4, 3, 4, 5]); ``` -Steve
May 01
parent Lance Bachmeier <no spam.net> writes:
On Wednesday, 1 May 2024 at 15:18:03 UTC, Steven Schveighoffer 
wrote:
 On Wednesday, 1 May 2024 at 01:09:33 UTC, Liam McGillivray 
 wrote:
 This is presumably such a common task that I'm surprised it 
 isn't easy to find the answer by searching;

 Is there a standard library function that removes all elements 
 from a dynamic array that matches an input argument?

 In `std.array` there's the `replace` function which is 
 supposed to replace all occurrences that match an input with 
 another. It seems to work as described on strings, but I get 
 compiler errors when using it on other array types. I've tried 
 using it to replace occurrences of a certain object in an 
 array with `[]` in order to remove all occurrences, but it's 
 not allowed.

 Is there a Phobos function that does what I want? It would be 
 crazy if there isn't.
`remove` https://dlang.org/phobos/std_algorithm_mutation.html#remove ```d arr = arr.remove!(v => shouldBeRemoved(v)); ``` Why the reassignment? Because `remove` removes elements *in place*, and does not change the range extents. It returns the portion of the range that contains the unremoved elements. So to give an example: ```d auto arr = [1, 2, 3, 4, 5]; auto result = arr.remove!(i => i % 2 == 1); // remove odd elements assert(result == [2, 4]); // first 2 are the slice that is stored in result // the last three are leftovers. assert(arr == [2, 4, 3, 4, 5]); ``` -Steve
In case anyone comes upon this in a search, I wanted to point out a couple dangers of using remove. The first is that it mutates arr, as shown in Steve's example. The second is ``` result[0] = 4; assert(result == [4, 4]); assert(arr == [2, 4, 3, 4, 5]); // Fails arr[0] = 2; assert(result == [4, 4]); // Fails ``` Any future changes you make to result or arr change the other. You can use remove to avoid the allocation of a new array, but you better be sure you never read or modify the original array again. If you use filter ``` auto result = arr.filter!(i => i % 2 == 0).array; ``` arr is unchanged and you can use arr and result as you want.
May 06