Course of Raku / Addendum 🆕 / Flow and functions / Subroutines and functional style / Exercises / A running accumulator
Solution: A running accumulator
Here is a possible solution to the task.
Code
sub make-accumulator($start) {
my $total = $start;
return -> $amount { $total += $amount };
}
my $acc = make-accumulator(100);
say $acc(10);
say $acc(25);
say $acc(-5);🦋 You can find the source code in the file accumulator.raku.
Output
110
135
130Comments
The returned block closes over
$total: that variable lives on between calls, so each call remembers the total from the previous one.$total += $amountboth updates the running sum and returns it, which is what eachsayprints:110, then135, then130.An alternative keeps the total inside the block itself, in a
statevariable. Unlike an ordinarymy, astatevariable is initialised only once — the first time the block runs — and then keeps its value across later calls:sub make-accumulator($start) { return -> $amount { state $total = $start; $total += $amount }; } my $acc = make-accumulator(100); say $acc(10); say $acc(25); say $acc(-5);
Each call to
make-accumulatorproduces a fresh block with its ownstate $total, so separate accumulators stay independent — and the closure over$startstill supplies each one its own starting value.