1
0

sample.go.txt 3.0 KB

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