Back to Lo

It Reducelast

docs/data/it-reducelast.md

1.53.0510 B
Original Source

Reduces a collection from right to left, returning a single value.

ReduceLast

go
result := it.ReduceLast(it.Range(1, 5), func(agg int, item int) int {
    return agg - item
}, 0)
// -10 (0 - 4 - 3 - 2 - 1)

ReduceLastI

Reduces a collection from right to left, returning a single value. The accumulator function includes the index.

go
result := it.ReduceLastI(it.Range(1, 5), func(agg int, item int, index int) int {
    return agg - item*index
}, 0)
// -20 (0 - 4*3 - 3*2 - 2*1 - 1*0)