github.com/AndrienkoAleksandr/go@v0.0.19/src/go/parser/testdata/slices.go2 (about)

     1  // Package slices implements various slice algorithms.
     2  package slices
     3  
     4  // Map turns a []T1 to a []T2 using a mapping function.
     5  func Map[T1, T2 any](s []T1, f func(T1) T2) []T2 {
     6  	r := make([]T2, len(s))
     7  	for i, v := range s {
     8  		r[i] = f(v)
     9  	}
    10  	return r
    11  }
    12  
    13  // Reduce reduces a []T1 to a single value using a reduction function.
    14  func Reduce[T1, T2 any](s []T1, initializer T2, f func(T2, T1) T2) T2 {
    15  	r := initializer
    16  	for _, v := range s {
    17  		r = f(r, v)
    18  	}
    19  	return r
    20  }
    21  
    22  // Filter filters values from a slice using a filter function.
    23  func Filter[T any](s []T, f func(T) bool) []T {
    24  	var r []T
    25  	for _, v := range s {
    26  		if f(v) {
    27  			r = append(r, v)
    28  		}
    29  	}
    30  	return r
    31  }