In python, there is a syntax called generator expression. For example, [i for i in range(1,5)], [i**2 for i in range(1,5)], and [i**2 for i in range(1,5) if i%2==0] would return [1, 2, 3, 4], [1, 4, 9, 16], and [4, 16], respectively. (Here [...something...] represents the list structure in python).
However, I wonder how would a MMA user do in MMA. What is the best (i.e. easy and elegant) equivalent way? First, I could only come up with these methods:
Range[4]
#^2& /@ Range[4]
#^2& /@ (Select[EvenQ]@Range[4])
(* output is the same as above*)
Is it good enough? Is there other way?
The first question does not concern how the memory used, while the second question does: I've heard that the generator expression will not directly create the whole list in the memory. So when the size of the data structure to be iterated is huge, the memory can be saved and prevent the program from crashing. In MMA, it seems that to micmick the same mechaism would require a explicit While loop and so on? How would you do?
Range[4]^2,Range[2,4,2]^2. But what you are probably looking for areTableandArray. – Henrik Schumacher Apr 04 '18 at 08:12