Re: Removing elements from dynamic arrays?

2022-04-06 Thread Siarhei Siamashka via Digitalmars-d-learn
On Wednesday, 6 April 2022 at 19:28:53 UTC, Steven Schveighoffer wrote: With `arr.find!(someLambda)`, if the lambda is using data from outside the lambda, it needs a closure, which means it may (probably does) allocate your needed data into a GC heap block that will then become garbage after

Re: Removing elements from dynamic arrays?

2022-04-06 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/6/22 2:32 PM, Salih Dincer wrote: On Wednesday, 6 April 2022 at 16:54:26 UTC, Steven Schveighoffer wrote: This is almost equivalent, but it requires a lambda and an allocation. So I'm not sure what thing you are trying to do here. **Source Code:**

Re: Removing elements from dynamic arrays?

2022-04-06 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 6 April 2022 at 16:54:26 UTC, Steven Schveighoffer wrote: This is almost equivalent, but it requires a lambda and an allocation. So I'm not sure what thing you are trying to do here. I tried to get these results but it didn't work: abc efg h [0, 3] [4, 7] [8, 9] abcefgh

Re: Removing elements from dynamic arrays?

2022-04-06 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/5/22 11:47 PM, Salih Dincer 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)

Re: Removing elements from dynamic arrays?

2022-04-05 Thread Salih Dincer via Digitalmars-d-learn
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

Re: Removing elements from dynamic arrays?

2022-04-05 Thread Steven Schveighoffer via Digitalmars-d-learn
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 ...;  

Re: Removing elements from dynamic arrays?

2022-04-05 Thread Paul Backus via Digitalmars-d-learn
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 =

Re: Removing elements from dynamic arrays?

2022-04-05 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/4/22 7:15 PM, Enjoys Math wrote: https://forum.dlang.org/post/eih04u$1463$1...@digitaldaemon.com A version of the code that takes `T which` as a parameter instead of `int index`. ``` // remove an item from an array template drop(T) {   T drop( inout T[] arr, T which ) Note to reader:

Re: Removing elements from dynamic arrays?

2022-04-04 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 4 April 2022 at 23:15:30 UTC, Enjoys Math wrote: ```d // remove an item from an array template drop(T) { T drop( inout T[] arr, T which ) { int i; T result; for (i=0; i < arr.length; i++) {

Re: Removing elements from dynamic arrays?

2022-04-04 Thread Ali Çehreli via Digitalmars-d-learn
On 4/4/22 16:15, Enjoys Math wrote: > https://forum.dlang.org/post/eih04u$1463$1...@digitaldaemon.com 2006 is a long time ago. :) > A version of the code that takes `T which` as a parameter instead of > `int index`. > > ``` > // remove an item from an array > template drop(T) > { >T drop(