I am learning about generator functions and in the documentation, generator functions are lazily evaluated but what is the benefit of this and why would you care? You will see this synchronous generator function:
Iterable<int> naturalsTo(int n) sync* {
int k = 0;
while (k < n) yield k++;
}
But what will be different if I do this?
Iterable<int> naturalsTo(int n) {
List<int> numbers = [];
for (int i = 0; i <= n; i++) {
numbers.add(i);
}
return numbers;
}
Both codes do exactly the same thing? Is there a memory advantage if I go for the generator function?