Thread overview
Removing an element from an array
Nov 10, 2022
Alexander Zhirov
Nov 10, 2022
H. S. Teoh
Nov 11, 2022
Alexander Zhirov
Nov 11, 2022
Alexander Zhirov
Nov 11, 2022
Alexander Zhirov
Nov 11, 2022
ag0aep6g
November 10, 2022

I have an array of self-written class A. I'm sorry for my tactlessness, but I'm confused about the modules. How do I correctly find a specific object fragment inside the array and delete it? I don't quite understand which modules to use to do this optimally.

A[] arr;
A fragment = new A;
...
arr.remove(fragment);  // Something like

In the pros, I would do it this way, for example via lambda

arr.erase(std::find_if(arr.cbegin(), arr.cend(), [&](const std::reference_wrapper<A> &item)
{
    return &item.get() == &fragment;
}));
November 10, 2022
On Thu, Nov 10, 2022 at 11:26:45PM +0000, Alexander Zhirov via Digitalmars-d-learn wrote:
> I have an array of self-written class `A`. I'm sorry for my tactlessness, but I'm confused about the modules. How do I correctly find a specific object `fragment` inside the array and delete it? I don't quite understand which modules to use to do this optimally.
> 
> ```d
> A[] arr;
> A fragment = new A;
> ...
> arr.remove(fragment);  // Something like
> ```

I would do something like this:

	// Warning: untested code
	import std.algorithm;
	arr = arr.remove(arr.countUntil(fragment));


T

-- 
Change is inevitable, except from a vending machine.
November 11, 2022

On Thursday, 10 November 2022 at 23:36:29 UTC, H. S. Teoh wrote:

>

On Thu, Nov 10, 2022 at 11:26:45PM +0000, Alexander Zhirov via Digitalmars-d-learn wrote:

>

I have an array of self-written class A. I'm sorry for my tactlessness, but I'm confused about the modules. How do I correctly find a specific object fragment inside the array and delete it? I don't quite understand which modules to use to do this optimally.

A[] arr;
A fragment = new A;
...
arr.remove(fragment);  // Something like

I would do something like this:

// Warning: untested code
import std.algorithm;
arr = arr.remove(arr.countUntil(fragment));

T

As always - simple and compact. Thank you:)

November 11, 2022

On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov wrote:

>
import std.algorithm;
arr = arr.remove(arr.countUntil(fragment));

And will this method work?

A[] arr;
A fragment = new A;
...
remove(current => current == fragment)(arr);
November 11, 2022

On Friday, 11 November 2022 at 05:36:37 UTC, Alexander Zhirov wrote:

>

On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov wrote:

>
import std.algorithm;
arr = arr.remove(arr.countUntil(fragment));

And will this method work?

A[] arr;
A fragment = new A;
...
remove(current => current == fragment)(arr);

And it will be even more accurate so as not to cause an error:

A[] arr;
A fragment = new A;
...
arr = remove(current => current == fragment)(arr);
November 11, 2022

On Friday, 11 November 2022 at 06:10:33 UTC, Alexander Zhirov wrote:

>

On Friday, 11 November 2022 at 05:36:37 UTC, Alexander Zhirov wrote:

>

On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov wrote:

>
import std.algorithm;
arr = arr.remove(arr.countUntil(fragment));

And will this method work?

A[] arr;
A fragment = new A;
...
remove(current => current == fragment)(arr);

And it will be even more accurate so as not to cause an error:

A[] arr;
A fragment = new A;
...
arr = remove(current => current == fragment)(arr);

You forgot the exclamation mark. And UFCS allows you to put arr in front, making it a bit easier on the eyes:

arr = arr.remove!(current => current == fragment);