Page MenuHomeFeedback Tracker

Add "<array> select <code>" to filter arrays, and "<array> apply <code>" to map/apply a function over array elements.
Closed, ResolvedPublic


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];


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.


Legacy ID
Feature Request

Event Timeline

Ian set Category to Feature Request.May 14 2015, 8:19 PM
Ian set Reproducibility to N/A.
Ian set Severity to None.
Ian set Resolution to Fixed.
Ian set Legacy ID to 142702281.May 8 2016, 12:05 PM
Ian edited a custom field.

select has been extended awhile ago, apply should be added in tomorrow's dev.

commy2 added a subscriber: commy2.May 8 2016, 12:05 PM
commy2 added a comment.Jan 5 2016, 8:00 PM

NVM, KK. I just read the post you made in the forums.