monaco-editor/3388.js

2 строки
3.2 KiB
JavaScript

"use strict";(self.webpackChunkmy_application=self.webpackChunkmy_application||[]).push([[3388],{3388:(n,t,e)=>{e.r(t),e.d(t,{default:()=>s});const s='// We often need our programs to perform operations on\n// collections of data, like selecting all items that\n// satisfy a given predicate or mapping all items to a new\n// collection with a custom function.\n\n// In some languages it\'s idiomatic to use [generic](http://en.wikipedia.org/wiki/Generic_programming)\n// data structures and algorithms. Go does not support\n// generics; in Go it\'s common to provide collection\n// functions if and when they are specifically needed for\n// your program and data types.\n\n// Here are some example collection functions for slices\n// of `strings`. You can use these examples to build your\n// own functions. Note that in some cases it may be\n// clearest to just inline the collection-manipulating\n// code directly, instead of creating and calling a\n// helper function.\n\npackage main\n\nimport "strings"\nimport "fmt"\n\n// Returns the first index of the target string `t`, or\n// -1 if no match is found.\nfunc Index(vs []string, t string) int {\n for i, v := range vs {\n if v == t {\n return i\n }\n }\n return -1\n}\n\n// Returns `true` if the target string t is in the\n// slice.\nfunc Include(vs []string, t string) bool {\n return Index(vs, t) >= 0\n}\n\n// Returns `true` if one of the strings in the slice\n// satisfies the predicate `f`.\nfunc Any(vs []string, f func(string) bool) bool {\n for _, v := range vs {\n if f(v) {\n return true\n }\n }\n return false\n}\n\n// Returns `true` if all of the strings in the slice\n// satisfy the predicate `f`.\nfunc All(vs []string, f func(string) bool) bool {\n for _, v := range vs {\n if !f(v) {\n return false\n }\n }\n return true\n}\n\n// Returns a new slice containing all strings in the\n// slice that satisfy the predicate `f`.\nfunc Filter(vs []string, f func(string) bool) []string {\n vsf := make([]string, 0)\n for _, v := range vs {\n if f(v) {\n vsf = append(vsf, v)\n }\n }\n return vsf\n}\n\n// Returns a new slice containing the results of applying\n// the function `f` to each string in the original slice.\nfunc Map(vs []string, f func(string) string) []string {\n vsm := make([]string, len(vs))\n for i, v := range vs {\n vsm[i] = f(v)\n }\n return vsm\n}\n\nfunc main() {\n\n // Here we try out our various collection functions.\n var strs = []string{"peach", "apple", "pear", "plum"}\n\n fmt.Println(Index(strs, "pear"))\n\n fmt.Println(Include(strs, "grape"))\n\n fmt.Println(Any(strs, func(v string) bool {\n return strings.HasPrefix(v, "p")\n }))\n\n fmt.Println(All(strs, func(v string) bool {\n return strings.HasPrefix(v, "p")\n }))\n\n fmt.Println(Filter(strs, func(v string) bool {\n return strings.Contains(v, "e")\n }))\n\n // The above examples all used anonymous functions,\n // but you can also use named functions of the correct\n // type.\n fmt.Println(Map(strs, strings.ToUpper))\n\n}\n'}}]);
//# sourceMappingURL=3388.js.map