My understanding is that Mathematica code executes faster when a list is operated on in a functional way (avoiding copies or splitting up the list).
I coded the following to toy with this idea, extracting the elements from a list of integers that satisfy a test-property (say Mod[x,7] == 0)
multiples7Test[x_] := (Mod[x, 7] == 0); myMapIndexed[f_, x_List] := MapIndexed[{f[#1], #2} &, x]; trueCase[x_] := TrueQ[x[[1]]]; pairs[x_] := myMapIndexed[multiples7Test, x]; multiples7Positions[x_] := Position[pairs[x], {_, {_}}?trueCase]; multiples7[x_] := Extract[x, multiples7Positions[x]] multiples7[Range[30]] produces {7, 14, 21, 28}.
Such code seems to stay close to the "whole-list" approach to gain efficiency. Presumably there is a Mathematica function that efficiently produces a list of elements (from a list of integers) satisfying a given property? I'd like to compare the speeds of various options to check how far the above code is off the (no doubt) highly optimised Mathematica version.
Select[Range[30], Mod[#, 7] == 0 &]? $\endgroup$