On 10/9/21 4:58 PM, Greg Strong wrote:
How do I filter some elements of an array into a new array?

This doesn't answer your specific question but std.algorithm.remove may be usable in some cases:

  https://dlang.org/phobos/std_algorithm_mutation.html#remove

If it matters, the following solution does not allocate new memory.

import std.stdio;
import std.algorithm;

void main() {
  auto arr = [ 1, 2, 3, 4, 5, 6, 7 ];

  // The default strategy is SwapStrategy.stable
  arr = arr.remove!(a => a % 2, SwapStrategy.unstable);

  writeln(arr);
}

The original array is modified. SwapStrategy.unstable will shuffle elements around. The program prints

[6, 2, 4]

Ali

Reply via email to