Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming

Generators

A generator is a routine that produces a series of values one at a time, yielding each on demand instead of building the whole list up front. Raku spells it with the gather / take pair: inside a gather block, every take hands one value to the resulting sequence, and the block pauses right there until the next value is pulled.

Because the values are produced lazily — only as they are asked for — a generator can even describe an endless series. This section shows how to build sequences with gather and take. The lower-level machinery that actually pulls those values out, one by one, is the subject of the next section, Iterators.

Topics in this section

Practice

Complete the quiz that covers the contents of this section.

Exercises

This section contains 3 exercises. Examine all the topics of this section before doing the coding practice.

  1. Gather the cubes
  2. Gather the multiples of three
  3. Gather with a condition

Course navigation

Solution: Sort with a feed   |   gather and take