CodeBlanks Logo Icon CodeBlanks

Pipeline with generators

PHP course · Expert · Lesson 6/54

Learn to chain multiple generators as a pipeline for data flow.

Generator pipelines

You can chain several generators: each generator takes the previous one's output and processes it. Data flows through; no intermediate arrays are built.

For example, one generator produces numbers, a second filters, a third transforms. Each step carries a single value, so large datasets are processed without loading into memory.

return and getReturn()

A generator can return a final value with return. This value is not yielded; after the generator has finished, you read it with getReturn().

This is useful to summarize the result of a stream. For example, you can yield numbers and at the end of the loop return $sum / $count; the average.

Exercise Filter and transform pipeline

Write filterEven(array $nums): \Generator: yield the even numbers. Write squares(array $nums): \Generator: iterate filterEven($nums) with foreach and yield the square of each value. Iterate squares([1, 2, 3, 4, 5, 6]) with foreach and print each square.

Show example solution
In filterEven, loop foreach ($nums as $n), if ($n % 2 === 0) yield $n;. In squares, loop foreach (filterEven($nums) as $n), yield $n * $n;. Then iterate foreach (squares([1, 2, 3, 4, 5, 6]) as $n) and echo $n . "\n";
Example solution:
<?php
function filterEven(array $nums): \Generator {
    foreach ($nums as $n) {
        if ($n % 2 === 0) {
            yield $n;
        }
    }
}

function squares(array $nums): \Generator {
    foreach (filterEven($nums) as $n) {
        yield $n * $n;
    }
}

foreach (squares([1, 2, 3, 4, 5, 6]) as $n) {
    echo $n . "\n";
}

Exercise Compute the average and read it with getReturn

Write average(array $nums): \Generator: yield each number, then return the average. Create $gen = average([10, 20, 30]), iterate with foreach to print each number. Then print the average with $gen->getReturn().

Show example solution
" . $gen->getReturn() . "\n";
Example solution:
<?php
function average(array $nums): \Generator {
    $sum = 0;
    $count = 0;
    foreach ($nums as $n) {
        $sum += $n;
        $count++;
        yield $n;
    }
    return $sum / $count;
}

$gen = average([10, 20, 30]);
foreach ($gen as $n) {
    echo $n . "\n";
}
echo "Average: " . $gen->getReturn() . "\n";

Test yourself

  1. What is the advantage of a generator pipeline?

    • Creates no intermediate arrays, saves memory
    • Runs faster because it is parallel
    • Loads all data into memory at once
    • Automates error handling
    Show answer

    Creates no intermediate arrays, saves memory — Correct. Each step carries a single value; no intermediate arrays are built, memory is preserved.

  2. Get the generator's final value

    $gen = counter();
    foreach ($gen as $n) { echo $n; }
    echo $gen->____();
    
    Show answer
    $gen = counter();
    foreach ($gen as $n) { echo $n; }
    echo $gen->getReturn();
    

All lessons in Expert