www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are there an equivalent to C#'s List in D's stdlib?

reply Jack <jckj33 gmail.com> writes:
I coduln't find an equivalent in the documentation, I could see 
appender, Array, container etc but none of has a Remove(T item) 

have to write one myself or I just couldn't find?

[1]: 
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.remove?view=net-5.0
Jan 07 2021
next sibling parent reply James Blachly <james.blachly gmail.com> writes:
On 1/7/21 9:53 PM, Jack wrote:
 I coduln't find an equivalent in the documentation, I could see 
 appender, Array, container etc but none of has a Remove(T item) method 

 one myself or I just couldn't find?
 
 [1]: 
 https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1
remove?view=net-5.0 
 
Speaking for me personally, I would try to work with Ranges[0], and in a functional style simply filter [1] out in the same way one calls for example: ```D-ish auto someRangeType = functionThatDoesWhatever(); auto rangeWithItemRemoved = someRangeType.filter(x => x.prop != whatever); ``` `Equals` such that the call to `Remove(new Part(){PartId=1534, PartName="cogs"});` considers only the PartId and does not match on name. Again, you could `filter(x => x.PartId != 1534)`, or you could, as in comparing, then `filter(x => x != new Part(1534, "cogs"))` Finally, although you asked about the standard library, there are many other container libraries, like emsi-containers [2] which do have List [0] http://ddili.org/ders/d.en/ranges.html [1] https://dlang.org/phobos/std_algorithm_iteration.html#filter [2] https://dlang-community.github.io/containers/index.html
Jan 07 2021
parent reply Jack <jckj33 gmail.com> writes:
On Friday, 8 January 2021 at 03:06:24 UTC, James Blachly wrote:
 On 1/7/21 9:53 PM, Jack wrote:
 I coduln't find an equivalent in the documentation, I could 
 see appender, Array, container etc but none of has a Remove(T 

 and I do have to write one myself or I just couldn't find?
 
 [1]: 
 https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.remove?view=net-5.0
 
Speaking for me personally, I would try to work with Ranges[0], and in a functional style simply filter [1] out in the same way for example: ```D-ish auto someRangeType = functionThatDoesWhatever(); auto rangeWithItemRemoved = someRangeType.filter(x => x.prop != whatever); ``` overloaded `Equals` such that the call to `Remove(new Part(){PartId=1534, PartName="cogs"});` considers only the PartId and does not match on name. Again, you could `filter(x => x.PartId != 1534)`, or you could, you are comparing, then `filter(x => x != new Part(1534, "cogs"))` Finally, although you asked about the standard library, there are many other container libraries, like emsi-containers [2] which do have List types that implement a `remove` function [0] http://ddili.org/ders/d.en/ranges.html [1] https://dlang.org/phobos/std_algorithm_iteration.html#filter [2] https://dlang-community.github.io/containers/index.html
quite a memory overhead, operator overloading, etc. Quite tedius, imo. Now, if I went to use filter, could I remove that item and somewhat get an array back, without perform a new array allocation? currently i'm with this, that as far i know, does perform memory allocation by array() call: arr = arr.filter!(x => x != value).array;
Jan 07 2021
parent Mike Parker <aldacron gmail.com> writes:
On Friday, 8 January 2021 at 03:22:49 UTC, Jack wrote:

 tedius, imo. Now, if I went to use filter, could I remove that 
 item and somewhat get an array back, without perform a new 
 array allocation? currently i'm with this, that as far i know, 
 does perform memory allocation by array() call:

 arr = arr.filter!(x => x != value).array;
See std.algorithm.mutation.remove. One overload takes an offset, but the other takes a predicate. Example from the docs: ``` static immutable base = [1, 2, 3, 2, 4, 2, 5, 2]; int[] arr = base[].dup; // using a string-based predicate writeln(remove!("a == 2")(arr)); // [1, 3, 4, 5] // The original array contents have been modified, // so we need to reset it to its original state. // The length is unmodified however. arr[] = base[]; // using a lambda predicate writeln(remove!(a => a == 2)(arr)); // [1, 3, 4, 5] ```
Jan 07 2021
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 8 January 2021 at 02:53:47 UTC, Jack wrote:
 I coduln't find an equivalent in the documentation, I could see 
 appender, Array, container etc but none of has a Remove(T item) 

 do have to write one myself or I just couldn't find?
SList and DList both have a function called linearRemoveElement. https://dlang.org/phobos/std_container_slist.html#.SList.linearRemoveElement https://dlang.org/phobos/std_container_dlist.html#.DList.linearRemoveElement
Jan 07 2021
parent Jack <jckj33 gmail.com> writes:
On Friday, 8 January 2021 at 03:14:34 UTC, Mike Parker wrote:
 On Friday, 8 January 2021 at 02:53:47 UTC, Jack wrote:
 I coduln't find an equivalent in the documentation, I could 
 see appender, Array, container etc but none of has a Remove(T 

 and I do have to write one myself or I just couldn't find?
SList and DList both have a function called linearRemoveElement. https://dlang.org/phobos/std_container_slist.html#.SList.linearRemoveElement https://dlang.org/phobos/std_container_dlist.html#.DList.linearRemoveElement
seems to do the job I want, thanks
Jan 07 2021