Collection Functions {.en}

收集功能 {.zh}

::: {.en} We often need our programs to perform operations on collections of data, like selecting all items that satisfy a given predicate or mapping all items to a new collection with a custom function. In some languages it’s idiomatic to use generic data structures and algorithms. Go does not support generics; in Go it’s common to provide collection functions if and when they are specifically needed for your program and data types. Here are some example collection functions for slices of strings. You can use these examples to build your own functions. Note that in some cases it may be clearest to just inline the collection-manipulating code directly, instead of creating and calling a helper function. :::

::: {.zh}

我们经常需要我们的程序来执行数据收集操作,例如选择满足给定谓词的所有项目或使用自定义函数将所有项目映射到newcollection。在某些语言中,使用[generic](http://en.wikipedia)是惯用的.org / wiki / Generic_programming)数据结构和算法。 Go不支持generics;在Go中,如果你的程序和数据类型特别需要它们,那么提供集合函数是很常见的。这里是一些用于sliceofchace的示例集合函数。您可以使用这些示例来构建自己的函数。请注意,在某些情况下,最简单的方法是直接内联集合操作代码,而不是创建和调用ahelper函数。

:::

  1. package main
  2. import "strings"
  3. import "fmt"

::: {.en} Index returns the first index of the target string t, or -1 if no match is found. :::

::: {.zh}

Index返回目标字符串t的第一个索引,如果未找到匹配则返回-1。

:::

  1. func Index(vs []string, t string) int {
  2. for i, v := range vs {
  3. if v == t {
  4. return i
  5. }
  6. }
  7. return -1
  8. }

::: {.en} Include returns true if the target string t is in the slice. :::

::: {.zh}

如果目标字符串t在theslice中,则包含返回“true”。

:::

  1. func Include(vs []string, t string) bool {
  2. return Index(vs, t) >= 0
  3. }

::: {.en} Any returns true if one of the strings in the slice satisfies the predicate f. :::

::: {.zh}

如果切片中的一个字符串满足谓词“f”,则任何返回“true”。

:::

  1. func Any(vs []string, f func(string) bool) bool {
  2. for _, v := range vs {
  3. if f(v) {
  4. return true
  5. }
  6. }
  7. return false
  8. }

::: {.en} All returns true if all of the strings in the slice satisfy the predicate f. :::

::: {.zh}

如果slice中的所有字符串都满足谓词f,则all返回true

:::

  1. func All(vs []string, f func(string) bool) bool {
  2. for _, v := range vs {
  3. if !f(v) {
  4. return false
  5. }
  6. }
  7. return true
  8. }

::: {.en} Filter returns a new slice containing all strings in the slice that satisfy the predicate f. :::

::: {.zh}

Filter返回一个新切片,其中包含满足谓词f的所有字符串。

:::

  1. func Filter(vs []string, f func(string) bool) []string {
  2. vsf := make([]string, 0)
  3. for _, v := range vs {
  4. if f(v) {
  5. vsf = append(vsf, v)
  6. }
  7. }
  8. return vsf
  9. }

::: {.en} Map returns a new slice containing the results of applying the function f to each string in the original slice. :::

::: {.zh}

Map返回一个新切片,其中包含将函数f应用于原始切片中每个字符串的结果。

:::

  1. func Map(vs []string, f func(string) string) []string {
  2. vsm := make([]string, len(vs))
  3. for i, v := range vs {
  4. vsm[i] = f(v)
  5. }
  6. return vsm
  7. }
  8. func main() {

::: {.en} Here we try out our various collection functions. :::

::: {.zh}

在这里,我们尝试各种收集功能。

:::

  1. var strs = []string{"peach", "apple", "pear", "plum"}
  2. fmt.Println(Index(strs, "pear"))
  3. fmt.Println(Include(strs, "grape"))
  4. fmt.Println(Any(strs, func(v string) bool {
  5. return strings.HasPrefix(v, "p")
  6. }))
  7. fmt.Println(All(strs, func(v string) bool {
  8. return strings.HasPrefix(v, "p")
  9. }))
  10. fmt.Println(Filter(strs, func(v string) bool {
  11. return strings.Contains(v, "e")
  12. }))

::: {.en} The above examples all used anonymous functions, but you can also use named functions of the correct type. :::

::: {.zh}

以上示例都使用了匿名函数,但您也可以使用correcttype的命名函数。

:::

  1. fmt.Println(Map(strs, strings.ToUpper))
  2. }
  1. $ go run collection-functions.go
  2. 2
  3. false
  4. true
  5. false
  6. [peach apple pear]
  7. [PEACH APPLE PEAR PLUM]