github.com/goplus/gop@v1.2.6/x/format/_testdata/collection/index.gop (about) 1 // We often need our programs to perform operations on 2 // collections of data, like selecting all items that 3 // satisfy a given predicate or mapping all items to a new 4 // collection with a custom function. 5 6 // In some languages it's idiomatic to use [generic](http://en.wikipedia.org/wiki/Generic_programming) 7 // data structures and algorithms. Go does not support 8 // generics; in Go it's common to provide collection 9 // functions if and when they are specifically needed for 10 // your program and data types. 11 12 // Here are some example collection functions for slices 13 // of `strings`. You can use these examples to build your 14 // own functions. Note that in some cases it may be 15 // clearest to just inline the collection-manipulating 16 // code directly, instead of creating and calling a 17 // helper function. 18 19 package main 20 21 import ( 22 "fmt" 23 "strings" 24 ) 25 26 // Index returns the first index of the target string `t`, or 27 // -1 if no match is found. 28 func Index(vs []string, t string) int { 29 for i, v := range vs { 30 if v == t { 31 return i 32 } 33 } 34 return -1 35 } 36 37 // Include returns `true` if the target string t is in the 38 // slice. 39 func Include(vs []string, t string) bool { 40 return Index(vs, t) >= 0 41 } 42 43 // Any returns `true` if one of the strings in the slice 44 // satisfies the predicate `f`. 45 func Any(vs []string, f func(string) bool) bool { 46 for _, v := range vs { 47 if f(v) { 48 return true 49 } 50 } 51 return false 52 } 53 54 // All returns `true` if all of the strings in the slice 55 // satisfy the predicate `f`. 56 func All(vs []string, f func(string) bool) bool { 57 for _, v := range vs { 58 if !f(v) { 59 return false 60 } 61 } 62 return true 63 } 64 65 // Filter returns a new slice containing all strings in the 66 // slice that satisfy the predicate `f`. 67 func Filter(vs []string, f func(string) bool) []string { 68 vsf := make([]string, 0) 69 for _, v := range vs { 70 if f(v) { 71 vsf = append(vsf, v) 72 } 73 } 74 return vsf 75 } 76 77 // Map returns a new slice containing the results of applying 78 // the function `f` to each string in the original slice. 79 func Map(vs []string, f func(string) string) []string { 80 vsm := make([]string, len(vs)) 81 for i, v := range vs { 82 vsm[i] = f(v) 83 } 84 return vsm 85 } 86 87 func main() { 88 89 // Here we try out our various collection functions. 90 var strs = []string{"peach", "apple", "pear", "plum"} 91 92 fmt.Println(Index(strs, "pear")) 93 94 fmt.Println(Include(strs, "grape")) 95 96 fmt.Println(Any(strs, func(v string) bool { 97 return strings.HasPrefix(v, "p") 98 })) 99 100 fmt.Println(All(strs, func(v string) bool { 101 return strings.HasPrefix(v, "p") 102 })) 103 104 fmt.Println(Filter(strs, func(v string) bool { 105 return strings.Contains(v, "e") 106 })) 107 108 // The above examples all used anonymous functions, 109 // but you can also use named functions of the correct 110 // type. 111 fmt.Println(Map(strs, strings.ToUpper)) 112 113 }