Task

Select certain elements from an Array into a new Array in a generic way.



To demonstrate, select all even numbers from an Array.

As an option, give a second solution which filters destructively,
by modifying the original Array rather than creating a new Array.




      REM Create the test array:
      items% = 1000
      DIM array%(items%)
      FOR index% = 1 TO items%
        array%(index%) = RND
      NEXT
 
      REM Count the number of filtered items:
      filtered% = 0
      FOR index% = 1 TO items%
        IF FNfilter(array%(index%)) filtered% += 1
      NEXT
 
      REM Create a new array containing the filtered items:
      DIM new%(filtered%)
      filtered% = 0
      FOR index% = 1 TO items%
        IF FNfilter(array%(index%)) THEN
          filtered% += 1
          new%(filtered%) = array%(index%)
        ENDIF
      NEXT
 
      REM Alternatively modify the original array:
      filtered% = 0
      FOR index% = 1 TO items%
        IF FNfilter(array%(index%)) THEN
          filtered% += 1
          array%(filtered%) = array%(index%)
        ENDIF
      NEXT
      END
 
      DEF FNfilter(A%) = ((A% AND 1) = 0)