Are there an equivalent to C#'s List in D's stdlib?

Jack jckj33 at gmail.com
Fri Jan 8 03:22:49 UTC 2021


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 
>> item) method like C#'s [1]. Are there not such implementation 
>> 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 
> one calls `Remove` in C#.
>
> for example:
>
> ```D-ish
> auto someRangeType = functionThatDoesWhatever();
>
> auto rangeWithItemRemoved = someRangeType.filter(x => x.prop != 
> whatever);
> ```
>
> In the C# docs, Remove<T> is called on a class which has 
> 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, 
> as in C#, overload the comparison operator for the class/struct 
> 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 
> just as in C#
>
> [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

That functional approach seems really better than C#'s, which is 
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;




More information about the Digitalmars-d-learn mailing list