The "<array> select <code>" variant would return a new array containing only the elements of "<array>" that "<code>" returns true on:
[1, 2, 3, 4, 5, 6] select { _x % 2 == 0 } // returns [2, 4, 6]
The new "<array> apply <code>" operator would call the "<code>" function on each element of "<array>", and return a new array with the results of each call. For example:
[1, 2, 3] apply { _x * 2 } // returns [2, 4, 6]
SQF has always had a version of "count" that worked with predicate functions, so it seems natural that the language would be able to filter a list with a <code> argument too.
These two new commands would replace the very common idiom of looping and building lists. For example, to filter a list in present-day SQF requires:
private "_tmp"; _tmp = []; { if (_x % 2 == 0) then { _tmp pushBack _x; }; } forEach [1, 2, 3, 4, 5, 6]; _tmp
The commands could also be optimised to avoid the multiple reallocations that existing filtering/applying code generally has due to the use of "pushBack":
- For small lists, "<array> select <code>" could first evaluate the predicates, storing the results in a bit-set (e.g. uint32_t), and then do a single exact array allocation before copying the filtered elements into the new array.
- For all cases, "<array> apply <code>" could allocate the new array with the exact number of required elements.