3 ms·
List comprehension is a bad idea, IMO. They were an improvement over not having anything like it, but python has improved over it (https://wiki.python.org/moin/
by Someone 9y ago
List comprehension is a bad idea, IMO. They were an improvement over not having anything like it, but python has improved over it (https://wiki.python.org/moin/Generators https://wiki.python.org/moin/Generators)
⇒ Generator expressions are the way to go. They would give you the sequence of elements without generating the (potentially enormous) data structure.
From there, methods to reify the items in a sequence would give you your list, array, or dictionary, where needed.
So, for Swift, I wouldn’t use
Array(1..<5, 3..<5) { ($0, $1) }
but the slightly longer
Array(for i in 1..<5, j in 3..<5 yield (i,j))
I’m not sure that is easy to fit in the existing parser, though. If it can be fit in, I would allow that code in ‘normal’ nested for loops, too.
- rbehrends 9y agoThis does not require a different syntax. You'd just construct a lazy list instead of an array. Neither is an improvement over the other. Sometimes it's more important to have the results up front fast, sometimes generating them on demand is better.