Thread overview
Are there an equivalent to C#'s List in D's stdlib?
Jan 08, 2021
Jack
Jan 08, 2021
James Blachly
Jan 08, 2021
Jack
Jan 08, 2021
Mike Parker
Jan 08, 2021
Mike Parker
Jan 08, 2021
Jack
January 08, 2021
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
January 07, 2021
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

January 08, 2021
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) method like C#'s [1]. Are there not such implementation 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
January 08, 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;


January 08, 2021
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 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?
>>
>
> 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
January 08, 2021
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]
```