github.com/enetx/g@v1.0.80/examples/iter/iter_fold.go (about)

     1  package main
     2  
     3  import "github.com/enetx/g"
     4  
     5  func main() {
     6  	// Create a slice of integers
     7  	is := g.SliceOf[g.Int](1, 2, 3, 4, 5)
     8  
     9  	// Transform the slice of integers into a slice of strings
    10  	itos := g.SliceMap(is, g.Int.ToString)
    11  
    12  	// Iterate over the transformed slice, perform folding, and print the result
    13  	itos.Iter().
    14  		Fold("0", // Initial accumulator value
    15  			func(acc, val g.String) g.String {
    16  				// Folding function: concatenate each element in the iterator with the accumulator
    17  				return g.Sprintf("(%s + %s)", acc, val)
    18  			}).
    19  		Print() // (((((0 + 1) + 2) + 3) + 4) + 5)
    20  }