Skip to content

Instantly share code, notes, and snippets.

@scottcagno
Created January 5, 2017 20:30
Show Gist options
  • Select an option

  • Save scottcagno/1ca9f559b3f8f2abb995c67a1849b5d3 to your computer and use it in GitHub Desktop.

Select an option

Save scottcagno/1ca9f559b3f8f2abb995c67a1849b5d3 to your computer and use it in GitHub Desktop.
Go Collection Functions
// Returns a new slice containing all strings in the slice that satisfy the predicate f.
func Filter(vs []string, f func(string) bool) []string {
vsf := make([]string, 0)
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}
// Returns a new slice containing the results of applying the function f to each string in the original slice.
func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment