www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Remove instance from array

reply Jolly James <j.j jmail.com> writes:
 WhatEver[] q = [];

 [...]

 auto i = new WhatEver();
 q[] = i;
How does one remove that instance 'i'?
Jul 05 2017
next sibling parent Andrea Fontana <nospam example.com> writes:
On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote:
 WhatEver[] q = [];

 [...]

 auto i = new WhatEver();
 q[] = i;
How does one remove that instance 'i'?
?
Jul 05 2017
prev sibling parent reply Igor Shirkalin <mathsoft inbox.ru> writes:
On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote:
 WhatEver[] q = [];

 [...]

 auto i = new WhatEver();
 q[] = i;
How does one remove that instance 'i'?
What exactly do you want to remove? After a[]=i your array contain a lot of references to 'i'.
Jul 05 2017
parent reply Jolly James <j.j jmail.com> writes:
On Wednesday, 5 July 2017 at 15:44:47 UTC, Igor Shirkalin wrote:
 On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote:
 WhatEver[] q = [];

 [...]

 auto i = new WhatEver();
 q[] = i;
How does one remove that instance 'i'?
What exactly do you want to remove? After a[]=i your array contain a lot of references to 'i'.
I would like to know how works: removing - the first - and all references to 'i' inside the 'q'.
Jul 05 2017
parent reply Igor Shirkalin <mathsoft inbox.ru> writes:
On Wednesday, 5 July 2017 at 15:48:14 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 15:44:47 UTC, Igor Shirkalin wrote:
 On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote:
 WhatEver[] q = [];

 [...]

 auto i = new WhatEver();
 q[] = i;
How does one remove that instance 'i'?
What exactly do you want to remove? After a[]=i your array contain a lot of references to 'i'.
I would like to know how works: removing - the first - and all references to 'i' inside the 'q'.
Perhaps, for all references to i it should look like: a = a.filter!(a => a !is i).array;
Jul 05 2017
parent reply Jolly James <j.j jmail.com> writes:
On Wednesday, 5 July 2017 at 15:56:45 UTC, Igor Shirkalin wrote:
 On Wednesday, 5 July 2017 at 15:48:14 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 15:44:47 UTC, Igor Shirkalin 
 wrote:
 On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote:
 WhatEver[] q = [];

 [...]

 auto i = new WhatEver();
 q[] = i;
How does one remove that instance 'i'?
What exactly do you want to remove? After a[]=i your array contain a lot of references to 'i'.
I would like to know how works: removing - the first - and all references to 'i' inside the 'q'.
Perhaps, for all references to i it should look like: a = a.filter!(a => a !is i).array;
Thank you! :) But why a containers so complicated in D? structs and classes, where I simply could call '.Remove(T item)' or '.RemoveAt(int index)'. I would know how this works, because the method names make sense, the docs are straight forward. Here in D everything looks like climbing mount everest. When you ask how to use D's containers you are recommended to use dynamic arrays instead. When you look at the docs for std.algorithm, e.g. the .remove section, you get bombed with things like 'SwapStrategy.unstable', asserts and tuples, but you aren't told how to simply remove 1 specific element.
Jul 05 2017
next sibling parent Igor Shirkalin <mathsoft inbox.ru> writes:
On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 15:56:45 UTC, Igor Shirkalin wrote:
 On Wednesday, 5 July 2017 at 15:48:14 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 15:44:47 UTC, Igor Shirkalin 
 wrote:
 On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote:
 WhatEver[] q = [];

 [...]

 auto i = new WhatEver();
 q[] = i;
How does one remove that instance 'i'?
What exactly do you want to remove? After a[]=i your array contain a lot of references to 'i'.
I would like to know how works: removing - the first - and all references to 'i' inside the 'q'.
Perhaps, for all references to i it should look like: a = a.filter!(a => a !is i).array;
Thank you! :) But why a containers so complicated in D? structs and classes, where I simply could call '.Remove(T item)' or '.RemoveAt(int index)'. I would know how this works, because the method names make sense, the docs are straight forward. Here in D everything looks like climbing mount everest. When you ask how to use D's containers you are recommended to use dynamic arrays instead. When you look at the docs for std.algorithm, e.g. the .remove section, you get bombed with things like 'SwapStrategy.unstable', asserts and tuples, but you aren't told how to simply remove 1 specific element.
I don't know c sharp, but I can tell everything about c++ and python. To climb a everest in python you have to know almost nothing, in c++ you have to know almost everything. In D you have to be smarter, you do not need to climb a everest but you have to know minimum to do that. Spend a year in learning and get the best result in minutes).
Jul 05 2017
prev sibling next sibling parent reply Jolly James <j.j jmail.com> writes:
On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 15:56:45 UTC, Igor Shirkalin wrote:
 [...]
Thank you! :) But why a containers so complicated in D? [...]
Part of CoreCLR's 'List<T>':
        public bool Remove(T item)
        {
            int index = IndexOf(item);
            if (index >= 0)
            {
                RemoveAt(index);
                return true;
            }

            return false;
        }
 // 
 https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Collections/Generic/List.cs
If there isn't already, maybe something similar to this should get part of Phobos. I think this could be really useful.
Jul 05 2017
parent reply Andrea Fontana <nospam example.com> writes:
On Wednesday, 5 July 2017 at 16:17:29 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 15:56:45 UTC, Igor Shirkalin 
 wrote:
 [...]
Thank you! :) But why a containers so complicated in D? [...]
Part of CoreCLR's 'List<T>':
        public bool Remove(T item)
        {
            int index = IndexOf(item);
            if (index >= 0)
            {
                RemoveAt(index);
                return true;
            }

            return false;
        }
 // 
 https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Collections/Generic/List.cs
If there isn't already, maybe something similar to this should get part of Phobos. I think this could be really useful.
q = q.remove(1); // Remove element with index 1 q = q.remove(x => x == instance); // Remove all items that match instance
Jul 06 2017
parent Jolly James <j.j jmail.com> writes:
On Thursday, 6 July 2017 at 08:15:10 UTC, Andrea Fontana wrote:
 On Wednesday, 5 July 2017 at 16:17:29 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote:
 [...]
Part of CoreCLR's 'List<T>':
 [...]
If there isn't already, maybe something similar to this should get part of Phobos. I think this could be really useful.
q = q.remove(1); // Remove element with index 1 q = q.remove(x => x == instance); // Remove all items that match instance
thx
Jul 06 2017
prev sibling next sibling parent reply bachmeier <no spam.net> writes:
On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote:

 Here in D everything looks like climbing mount everest. When 
 you ask how to use D's containers you are recommended to use 
 dynamic arrays instead. When you look at the docs for 
 std.algorithm, e.g. the .remove section, you get bombed with 
 things like 'SwapStrategy.unstable', asserts and tuples, but 
 you aren't told how to simply remove 1 specific element.
If you feel that there is a problem with the docs, you should file a bug: https://dlang.org/bugstats.php The documentation is still not perfect, but the only way to improve it is to file bugs when you see something that needs fixing.
Jul 05 2017
parent reply Jolly James <j.j jmail.com> writes:
On Wednesday, 5 July 2017 at 16:55:43 UTC, bachmeier wrote:
 On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote:

 Here in D everything looks like climbing mount everest. When 
 you ask how to use D's containers you are recommended to use 
 dynamic arrays instead. When you look at the docs for 
 std.algorithm, e.g. the .remove section, you get bombed with 
 things like 'SwapStrategy.unstable', asserts and tuples, but 
 you aren't told how to simply remove 1 specific element.
If you feel that there is a problem with the docs, you should file a bug: https://dlang.org/bugstats.php The documentation is still not perfect, but the only way to improve it is to file bugs when you see something that needs fixing.
unfortunately, it's not that the docs would be wrong or something that can be easily corrected. Nope, the docs do everything right, they show you what the existing things do. But what they don't do is how to get stuff done. imho some additional, useful guides would be nice.
Jul 05 2017
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Wed, Jul 05, 2017 at 05:07:14PM +0000, Jolly James via Digitalmars-d-learn
wrote:
 On Wednesday, 5 July 2017 at 16:55:43 UTC, bachmeier wrote:
 On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote:
 
 Here in D everything looks like climbing mount everest. When you
 ask how to use D's containers you are recommended to use dynamic
 arrays instead. When you look at the docs for std.algorithm, e.g.
 the .remove section, you get bombed with things like
 'SwapStrategy.unstable', asserts and tuples, but you aren't told
 how to simply remove 1 specific element.
If you feel that there is a problem with the docs, you should file a bug: https://dlang.org/bugstats.php The documentation is still not perfect, but the only way to improve it is to file bugs when you see something that needs fixing.
unfortunately, it's not that the docs would be wrong or something that can be easily corrected. Nope, the docs do everything right, they show you what the existing things do. But what they don't do is how to get stuff done. imho some additional, useful guides would be nice.
No, that's a sign that the docs are not good enough. Describing what existing things do is only the bare minimum of documentation. *Good* documentation should also tell you what you can use it for, and give examples of how to do it, preferably examples that address the most common use cases first, and then, if necessary, a discussion of more details later. The very first example in the docs for std.algorithm.mutation.remove is not suitable as a first example, because it already throws something unexpected in your face, i.e., that remove() doesn't change the incoming range directly. It should have shown the *correct* way of removing an element as a first example, i.e.: a = a.remove(1); And *then* talk about the details later. Here's my fix for this: https://github.com/dlang/phobos/pull/5548 In the future, please do file a bug for these kinds of documentation issues. D needs *good* documentation, not just bare-minimum documentation. T -- Long, long ago, the ancient Chinese invented a device that lets them see through walls. It was called the "window".
Jul 05 2017
prev sibling parent Igor Shirkalin <mathsoft inbox.ru> writes:
On Wednesday, 5 July 2017 at 16:04:16 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 15:56:45 UTC, Igor Shirkalin wrote:
 On Wednesday, 5 July 2017 at 15:48:14 UTC, Jolly James wrote:
 On Wednesday, 5 July 2017 at 15:44:47 UTC, Igor Shirkalin 
 wrote:
 On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote:
 WhatEver[] q = [];

 [...]

 auto i = new WhatEver();
 q[] = i;
How does one remove that instance 'i'?
What exactly do you want to remove? After a[]=i your array contain a lot of references to 'i'.
I would like to know how works: removing - the first - and all references to 'i' inside the 'q'.
Perhaps, for all references to i it should look like: a = a.filter!(a => a !is i).array;
Thank you! :) But why a containers so complicated in D? structs and classes, where I simply could call '.Remove(T item)' or '.RemoveAt(int index)'. I would know how this works, because the method names make sense, the docs are straight forward. Here in D everything looks like climbing mount everest. When you ask how to use D's containers you are recommended to use dynamic arrays instead. When you look at the docs for std.algorithm, e.g. the .remove section, you get bombed with things like 'SwapStrategy.unstable', asserts and tuples, but you aren't told how to simply remove 1 specific element.
I don't know c sharp, but I can tell everything about c++ and python. To climb a everest in python you have to know almost nothing, in c++ you have to know almost everything. In D you have to be smarter, you do not need to climb a everest but you have to know minimum to do that. Spend a year in learning and get the best result in minutes).
Aug 05 2017