I am trying to avoid using For because I heard that For is not cool in functional programming. Below is a problem that the first idea came to my mind is using For. How can I change this into a code using Table or Range or something else?
What the code does is to extract the elements in a list ls which satisfies certain condition into a new list lsNew.
ls = RandomInteger[20, 1000];
lsNew = {};
For[i = 1, i <= Length[ls], i++,
If[ls[[i]] < 6, lsNew = Append[lsNew, ls[[i]]]]]
lsNew
Selectinstead. The setup is much, much simpler, and it doesn't use $O(n^2)$ methods to do it. (Appendused like this is the culprit.) – rcollyer Dec 29 '15 at 03:38