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

     1  package main
     2  
     3  import "github.com/enetx/g"
     4  
     5  func main() {
     6  	// Example 1: Map each string in the slice to its uppercase version and print the result
     7  	g.SliceOf[g.String]("", "bbb", "ddd", "", "aaa", "ccc").
     8  		Iter().
     9  		Map(g.String.Upper). // Map each string to its uppercase version
    10  		Collect().
    11  		Print() // Slice[, BBB, DDD, , AAA, CCC]
    12  
    13  	// Example 2: Map each string in the slice, replacing empty strings with "abc", and print the result
    14  	g.SliceOf[g.String]("", "bbb", "ddd", "", "aaa", "ccc").
    15  		Iter().
    16  		Map(func(s g.String) g.String {
    17  			if s.Empty() {
    18  				s = "abc"
    19  			}
    20  			return s
    21  		}).
    22  		Collect().
    23  		Print() // Slice[abc, bbb, ddd, abc, aaa, ccc]
    24  }