Created
January 5, 2017 20:30
-
-
Save scottcagno/1ca9f559b3f8f2abb995c67a1849b5d3 to your computer and use it in GitHub Desktop.
Go Collection Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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