digitalmars.D.learn - Removing an element from an array
- Alexander Zhirov (19/19) Nov 10 2022 I have an array of self-written class `A`. I'm sorry for my
- H. S. Teoh (8/19) Nov 10 2022 I would do something like this:
- Alexander Zhirov (2/21) Nov 10 2022 As always - simple and compact. Thank you:)
- Alexander Zhirov (9/13) Nov 10 2022 And will this method work?
- Alexander Zhirov (9/22) Nov 10 2022 And it will be even more accurate so as not to cause an error:
- ag0aep6g (5/29) Nov 11 2022 You forgot the exclamation mark. And UFCS allows you to put `arr`
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 ``` In the pros, I would do it this way, for example via lambda ```c++ arr.erase(std::find_if(arr.cbegin(), arr.cend(), [&](const std::reference_wrapper<A> &item) { return &item.get() == &fragment; })); ```
Nov 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.
Nov 10 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:As always - simple and compact. Thank you:)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
Nov 10 2022
On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov wrote:```d import std.algorithm; arr = arr.remove(arr.countUntil(fragment)); ```And will this method work? ```d A[] arr; A fragment = new A; ... remove(current => current == fragment)(arr); ```
Nov 10 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:And it will be even more accurate so as not to cause an error: ```d A[] arr; A fragment = new A; ... arr = remove(current => current == fragment)(arr); ``````d import std.algorithm; arr = arr.remove(arr.countUntil(fragment)); ```And will this method work? ```d A[] arr; A fragment = new A; ... remove(current => current == fragment)(arr); ```
Nov 10 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: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);On Friday, 11 November 2022 at 00:02:09 UTC, Alexander Zhirov wrote:And it will be even more accurate so as not to cause an error: ```d A[] arr; A fragment = new A; ... arr = remove(current => current == fragment)(arr); ``````d import std.algorithm; arr = arr.remove(arr.countUntil(fragment)); ```And will this method work? ```d A[] arr; A fragment = new A; ... remove(current => current == fragment)(arr); ```
Nov 11 2022