Create array from range
Ali Çehreli
acehreli at yahoo.com
Sun Oct 10 03:04:57 UTC 2021
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
More information about the Digitalmars-d-learn
mailing list