Say I have a DList. I am looking for a vanilla way to insert/remove elements in place while traversing that list.

```d
import std.container : DList;

struct myType{
   int id;
   // [...] other stuff
}

auto list = DList!myType();

// [...] populate list with elements
```

To remove the element with id=3 I can do:
```d
list.linearRemove(list[].find!(a => a.id==3).take(1));
```
but according to [this post](https://forum.dlang.org/post/mailman.3110.1356864469.5162.digitalmar...@puremagic.com) linearRemove might be less efficient than remove and remove can't be used. Also in the example above, finding the needle is easy but my case would involve more complicated scenarios that I am relunctant to bury into find's predicate.

I would like to do something like traversing a DList, operating on the current element, and potentially removing that element or inserting a new one before/after it - an easy operation if you code a DList yourself. Maybe I missed something?

```d
// Traverse and operate on current element
foreach(element; list)
{
// [...] do something long and complicated with m, throw a random number rand

    if(rand < 0.5)
        list.removeInPlace(); // remove 'element'
    else
list.insertInPlace(myType(...)); // insert a new element "here" (before/after 'element')
}
```

Any way to achieve this simply with D? E.g. is there a way to extract the 'reading head' of a list being traversed and to use that head as a range into the remove, insertAfter, insertBefore functions?

Reply via email to