An example:
for(x <- c1; y <- c2; z <- c3) yield {...}
which can be translated into:
c1.flatMap(x => c2.flatMap(y => c3.map(z => {...})))
or:
for(x <- c1) for (y <- c2) for (z <- c3) yield {...}
Time complexity is O(c1 * c2 * c3) according to the second translation, what if c1, c2, c3 are very big numbers?
I tried to search on the internet, and I found there are many codes who have much more generators than this example.
Question: In other languages, we want to avoid nested for loops, but why scala designed this mechanism, dose it have some special tricks to make a very good performance for the nested loop ?
If I misunderstand something, please tell me, I would like to know better the for-comprehension in scala. Thank you.
O(c1* c2*c3)time, and no language with any other means will deliver better. But I feel like you perceive for comprehension as smthng identical to loops while in fact it is not. For example, for collections likeListthis may be recursion, for things different than collections this will be altogether different thing. - Alexander Arendar