mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 09:48:19 +01:00
fn: add reduce func
This commit is contained in:
parent
d702158d9b
commit
f166829fe4
1 changed files with 17 additions and 0 deletions
17
fn/func.go
Normal file
17
fn/func.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package fn
|
||||||
|
|
||||||
|
// Reducer represents a function that takes an accumulator and the value, then
|
||||||
|
// returns a new accumulator.
|
||||||
|
type Reducer[T, V any] func(accum T, value V) T
|
||||||
|
|
||||||
|
// Reduce takes a slice of something, and a reducer, and produces a final
|
||||||
|
// accumulated value.
|
||||||
|
func Reduce[T any, V any, S []V](s S, f Reducer[T, V]) T {
|
||||||
|
var accum T
|
||||||
|
|
||||||
|
for _, x := range s {
|
||||||
|
accum = f(accum, x)
|
||||||
|
}
|
||||||
|
|
||||||
|
return accum
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue