Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Reduction / The reduce routine

Quiz — Reduction

What does the following program print?

say (2, 5, 8).reduce(-> $acc, $x { $acc * 10 + $x });
015
1258
080
02058

The block carries a running result $acc and the next element $x. Starting from 2, each step does $acc * 10 + $x: 2, then 2*10+5 = 25, then 25*10+8 = 258. This is the flexible side of reduce — the block can be any combining rule, not just a single operator.

Course navigation

The reduce routine   |   Sum a list