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

Chaining feeds

The real value of feeds appears when you have several stages. Each ==> passes its result to the next operation, so a whole pipeline reads top to bottom in the order the work happens:

(1..10)
    ==> grep(* %% 2)
    ==> map(* ** 2)
    ==> my @result;

say @result; # [4 16 36 64 100]

Follow the data down the page: start with 1..10, keep the even numbers, square each of them, and collect the result. The even numbers are 2, 4, 6, 8, 10, and their squares are 4, 16, 36, 64, 100.

Written as nested calls, the same pipeline would be (1..10).grep(* %% 2).map(* ** 2) — correct, but read inside-out. The feed version lists the steps in execution order, which is often easier to write and to read, especially as the chain grows.

Feeds and method chains do the same job; choose whichever makes a particular transformation clearest.

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Quiz — The feed operator   |   Quiz — Feeds


💪 Or jump directly to the exercises in this section.