Created
April 30, 2014 22:55
-
-
Save anonymous/0de5f631a74c77a3da33 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import "fmt" | |
import "strings" | |
// some ruby #map type things, in golang | |
// mostly taken from https://groups.google.com/forum/#!topic/golang-nuts/fgaeXCUEPC4 | |
// apparently calling this out with specific types is faster than defining it generically to use interfaces: http://comments.gmane.org/gmane.comp.lang.go.general/43076 | |
func _map(coll []string, f func(string) string) []string { | |
var newColl = make([]string, len(coll)) | |
for i, s := range coll { | |
newColl[i] = f(s) | |
} | |
return newColl | |
} | |
func main() { | |
// map | |
array := []string{"a", "b", "c"} | |
array2 := make([]string, len(array)) | |
for i, s := range array { array2[i] = strings.ToUpper(s) } | |
fmt.Println(array2) | |
// map! | |
array = []string{"a", "b", "c"} | |
for i, s := range array { array2[i] = strings.ToUpper(s) } | |
fmt.Println(array2) | |
// local map method | |
array = []string{"a", "b", "c"} | |
array2 = _map(array, strings.ToUpper) | |
fmt.Println(array2) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment