reductions#

Like reduce, but also return every intermediate accumulator value.

Synopsis#

my @steps = reductions { BLOCK } @list;
my @running = reductions { $a + $b } 1..4;   # (1, 3, 6, 10)

The block is called in the same way as for reduce: $a is the accumulator, $b is the next element. reductions differs in that it captures the accumulator at every step rather than just the final value.

What you get back#

A list whose first element is the first input value and whose subsequent elements are each block invocation’s return value in order. The last element equals what reduce would have returned for the same block and list.

Examples#

reduce     { "$a-$b" }  'a'..'d';    # 'a-b-c-d'
reductions { "$a-$b" }  'a'..'d';    # ('a', 'a-b', 'a-b-c', 'a-b-c-d')
my @running_sum = reductions { $a + $b } @values;

Differences from upstream#

Fully compatible with upstream.

See also#

  • reduce — when only the final result is needed.