Suppose I'd like to generate a table of integers:
Table[i, {i, 10}]
Great; now suppose I want only the integers that are even
Select[Table[i, {i, 10}], EvenQ]
This is fine for small things, but if I'd like to iterate over say $2^{n}$ things, only maybe $n$ of which will satisfy the predicate, it's a pretty terrible way to do it.
Is there some good idiomatic way to do it? I could use a Do loop (I guess with Appending?) but there must be a better way, right?
Sow[]/Reap[]withDo[]; e.g.Reap[Do[If[EvenQ[k], Sow[k]], {k, 10}]][[-1, 1]]. – J. M.'s missing motivation Mar 23 '13 at 13:13Selectoption would do ...) – Noon Silk Mar 23 '13 at 13:27Table[2 i, {i, 1, 5}]orTable[i, {i, 2, 10, 2}]for instance; so, exploit patterns when you can! – J. M.'s missing motivation Mar 23 '13 at 13:32Selectis slow (I believe) because it creates an inappropriately large array.DowithReap/Sowalso seems unfortunately slow. I was hoping there was a more idomatic (and so hopefully fast) way. – Noon Silk Mar 23 '13 at 13:332 Range[Quotient[len,2]], or evenRange[2,len,2]does the same asSelect[Table[i,{i,len}],EvenQ]– Sasha Mar 23 '13 at 13:36Truewhen passed an integer. – Noon Silk Mar 23 '13 at 13:39Table[...,{i, {list of ints that meet predicate}]. – rcollyer Mar 23 '13 at 16:22