reductions#

Like reduce, but also return every intermediate accumulator value.

תקציר#

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.

מה מוחזר#

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.

דוגמאות#

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;

הבדלים מן ה-upstream#

Fully compatible with upstream.

ראו גם#

  • reduce - when only the final result is needed.