github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xslice/fold.go (about)

     1  package xslice
     2  
     3  func FoldLeft(a []interface{}, fn func(left, right interface{}) interface{}, initial interface{}) interface{} {
     4  	for _, x := range a {
     5  		initial = fn(initial, x)
     6  	}
     7  	return initial
     8  }
     9  
    10  func FoldRight(a []interface{}, fn func(left, right interface{}) interface{}, initial interface{}) interface{} {
    11  	n := len(a)
    12  	for i := n - 1; i >= 0; i-- {
    13  		initial = fn(initial, a[i])
    14  	}
    15  	return initial
    16  }