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 Append
ing?) but there must be a better way, right?
Sow[]
/Reap[]
withDo[]
; e.g.Reap[Do[If[EvenQ[k], Sow[k]], {k, 10}]][[-1, 1]]
. $\endgroup$Select
option would do ...) $\endgroup$Table[2 i, {i, 1, 5}]
orTable[i, {i, 2, 10, 2}]
for instance; so, exploit patterns when you can! $\endgroup$