Part 16 - Generators
Generator Expressions have similar syntax to the
for loops that we have covered, and serve a similar purpose.
The best way to learn how to use Generator Expressions is by example, so here we load up a
The cat-weight example is probably what Generator Expressions are most useful for.
You don't have to create
Lists from them either, that's mostly for show.
generators are derived from
IEnumerable, so you get all the niceties of the
for loop as well.
A Generator Method is like a regular method that can return multiple times.
Here's a Generator Method that will return exponents of 2.
Generator Methods are very powerful because they keep all their local variables in memory after a yield. This can allow for certain programming techniques not found in some other languages.
Generators are very powerful and useful.
- Create a Generator that will destroy mankind.
Go on to Part 17 - Macros