Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 18, 2018 How to delete element from array container or dlist? | ||||
---|---|---|---|---|
| ||||
In DList we have method remove, but I can't understand how this method works, I want write somethink like this:
> void unsubscribe(EventsSubscriber subscriber) {
> subscribers.remove(subscriber);
> }
|
March 18, 2018 Re: How to delete element from array container or dlist? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey Kabylin | On Sunday, 18 March 2018 at 14:58:52 UTC, Andrey Kabylin wrote:
> In DList we have method remove, but I can't understand how this method works, I want write somethink like this:
>> void unsubscribe(EventsSubscriber subscriber) {
>> subscribers.remove(subscriber);
>> }
The remove function seems to expect an index, not an element.
|
March 18, 2018 Re: How to delete element from array container or dlist? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey Kabylin | On Sunday, 18 March 2018 at 14:58:52 UTC, Andrey Kabylin wrote: > In DList we have method remove, but I can't understand how this method works, I want write somethink like this: >> void unsubscribe(EventsSubscriber subscriber) { >> subscribers.remove(subscriber); >> } So I guess you would want something like subscribers.remove!(a => a == subscriber)); which is the different definition of remove available here: https://dlang.org/phobos/std_algorithm_mutation.html#.remove.2 |
March 18, 2018 Re: How to delete element from array container or dlist? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael | On Sunday, 18 March 2018 at 15:32:47 UTC, Michael wrote:
> On Sunday, 18 March 2018 at 14:58:52 UTC, Andrey Kabylin wrote:
>> In DList we have method remove, but I can't understand how this method works, I want write somethink like this:
>>> void unsubscribe(EventsSubscriber subscriber) {
>>> subscribers.remove(subscriber);
>>> }
>
> So I guess you would want something like
>
> subscribers.remove!(a => a == subscriber));
>
> which is the different definition of remove available here:
> https://dlang.org/phobos/std_algorithm_mutation.html#.remove.2
Yes this works, thanks!
|
March 18, 2018 Re: How to delete element from array container or dlist? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey Kabylin | On Sunday, 18 March 2018 at 15:42:18 UTC, Andrey Kabylin wrote:
> On Sunday, 18 March 2018 at 15:32:47 UTC, Michael wrote:
>> On Sunday, 18 March 2018 at 14:58:52 UTC, Andrey Kabylin wrote:
>>> In DList we have method remove, but I can't understand how this method works, I want write somethink like this:
>>>> void unsubscribe(EventsSubscriber subscriber) {
>>>> subscribers.remove(subscriber);
>>>> }
>>
>> So I guess you would want something like
>>
>> subscribers.remove!(a => a == subscriber));
>>
>> which is the different definition of remove available here:
>> https://dlang.org/phobos/std_algorithm_mutation.html#.remove.2
>
> Yes this works, thanks!
No problem, glad to help!
|
April 26, 2019 Re: How to delete element from array container or dlist? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael | On Sunday, 18 March 2018 at 16:14:06 UTC, Michael wrote:
> On Sunday, 18 March 2018 at 15:42:18 UTC, Andrey Kabylin wrote:
>> On Sunday, 18 March 2018 at 15:32:47 UTC, Michael wrote:
>>> On Sunday, 18 March 2018 at 14:58:52 UTC, Andrey Kabylin wrote:
>>>> In DList we have method remove, but I can't understand how this method works, I want write somethink like this:
>>>>> void unsubscribe(EventsSubscriber subscriber) {
>>>>> subscribers.remove(subscriber);
>>>>> }
>>>
>>> So I guess you would want something like
>>>
>>> subscribers.remove!(a => a == subscriber));
>>>
>>> which is the different definition of remove available here:
>>> https://dlang.org/phobos/std_algorithm_mutation.html#.remove.2
>>
>> Yes this works, thanks!
>
> No problem, glad to help!
Just an FYI:
This didn't work as I expected -- the length of subscribers didn't change. What I needed here was
subscribers = subscribers.remove!(a => a == subscriber));
Otherwise this sort of behaviour resulted:
// code:
struct A {int i;}
A[] as = [A(1), A(2), A(3)];
writeln("as = ", as);
writeln(remove!(a => a == A(2))(as));
writeln("as = ", as);
writeln(remove!(a => a == A(3))(as));
writeln("as = ", as);
// output:
as = [A(1), A(2), A(3)]
[A(1), A(3)]
as = [A(1), A(3), A(3)]
[A(1)]
as = [A(1), A(3), A(3)]
|
Copyright © 1999-2021 by the D Language Foundation