On 4/5/22 11:43 AM, Paul Backus wrote:
On Tuesday, 5 April 2022 at 14:10:44 UTC, Steven Schveighoffer wrote:
I'd implement it probably like this (for D2):

```d
auto drop(T)(ref T[] arr, T which)
{
   import std.algorithm, std.range;
   auto f = arr.find(which);
   debug if(f.empty) throw ...;
   auto result = arr.front;
   arr = arr.remove(&f[0] - &arr[0]); // god I hate this
   return result;
}
```

I think you can get rid of the ugly pointer arithmetic using `countUntil`:

```d
auto drop(T)(ref T[] arr, T which)
{
     import std.algorithm, std.range, std.exception;

     auto i = arr.countUntil(which);
     debug enforce(i < arr.length, "Not found");
     auto result = arr[i];
     arr = arr.remove(i);
     return result;
}
```

Yeah, or use enumerate. But it's painful:

```d
auto f = arr.enumerate.find!((v, w) => v[1] == w)(which);
auto result = f.front[1];
arr = arr.remove(result[0]);
return result;
```

I have a lib somewhere which isn't complete that allows remembering indexes for elements so you can tease out the original index, but it breaks when you use it on strings (of course).

-Steve

Reply via email to