github.com/goplus/gop@v1.2.6/x/format/_testdata/collection/format.expect (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  import (
    20  	"strings"
    21  )
    22  
    23  // Index returns the first index of the target string `t`, or
    24  // -1 if no match is found.
    25  func Index(vs []string, t string) int {
    26  	for i, v := range vs {
    27  		if v == t {
    28  			return i
    29  		}
    30  	}
    31  	return -1
    32  }
    33  
    34  // Include returns `true` if the target string t is in the
    35  // slice.
    36  func Include(vs []string, t string) bool {
    37  	return Index(vs, t) >= 0
    38  }
    39  
    40  // Any returns `true` if one of the strings in the slice
    41  // satisfies the predicate `f`.
    42  func Any(vs []string, f func(string) bool) bool {
    43  	for _, v := range vs {
    44  		if f(v) {
    45  			return true
    46  		}
    47  	}
    48  	return false
    49  }
    50  
    51  // All returns `true` if all of the strings in the slice
    52  // satisfy the predicate `f`.
    53  func All(vs []string, f func(string) bool) bool {
    54  	for _, v := range vs {
    55  		if !f(v) {
    56  			return false
    57  		}
    58  	}
    59  	return true
    60  }
    61  
    62  // Filter returns a new slice containing all strings in the
    63  // slice that satisfy the predicate `f`.
    64  func Filter(vs []string, f func(string) bool) []string {
    65  	vsf := make([]string, 0)
    66  	for _, v := range vs {
    67  		if f(v) {
    68  			vsf = append(vsf, v)
    69  		}
    70  	}
    71  	return vsf
    72  }
    73  
    74  // Map returns a new slice containing the results of applying
    75  // the function `f` to each string in the original slice.
    76  func Map(vs []string, f func(string) string) []string {
    77  	vsm := make([]string, len(vs))
    78  	for i, v := range vs {
    79  		vsm[i] = f(v)
    80  	}
    81  	return vsm
    82  }
    83  
    84  // Here we try out our various collection functions.
    85  var strs = []string{"peach", "apple", "pear", "plum"}
    86  
    87  println Index(strs, "pear")
    88  
    89  println Include(strs, "grape")
    90  
    91  println Any(strs, func(v string) bool {
    92  	return strings.hasPrefix(v, "p")
    93  })
    94  
    95  println All(strs, func(v string) bool {
    96  	return strings.hasPrefix(v, "p")
    97  })
    98  
    99  println Filter(strs, func(v string) bool {
   100  	return strings.contains(v, "e")
   101  })
   102  
   103  // The above examples all used anonymous functions,
   104  // but you can also use named functions of the correct
   105  // type.
   106  println Map(strs, strings.ToUpper)