A generator is an executable entity (like a function or procedure) that contains code that yields a sequence of values, one at a time, so that each time you call the generator, the next value in the sequence is provided. Generators are often built on top of coroutines or objects so that the internal state of the object is handled “naturally”. Generators are often used in situations where a sequence is potentially infinite, and where it is possible to construct the next value of the sequence with only minimal state.
- Create a function that returns a generation of the m’th powers of the positive integers starting from zero, in order, and without obvious or simple upper limit. (Any upper limit to the generator should not be stated in the source but should be down to factors such as the languages natural integer size limit or computational time/size).
- Use it to create a generator of:
- Create a new generator that filters all cubes from the generator of squares.
- Drop the first 20 values from this last generator of filtered results then show the next 10 values
Note that this task requires the use of generators in the calculation of the result.
A templated solution:
20: 529 21: 576 22: 625 23: 676 24: 784 25: 841 26: 900 27: 961 28: 1024 29: 1089
Content is available under GNU Free Documentation License 1.2.