Created
June 2, 2013 09:46
-
-
Save uraimo/5693171 to your computer and use it in GitHub Desktop.
A Tour of Go: Maps Exercise solution w/o strings.Fields
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 ( | |
"code.google.com/p/go-tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
res := make(map[string]int); | |
str := make([]rune,0,10) | |
for _, r := range(s){ | |
if r == ' ' { | |
res[string(str)]++ | |
str = make([]rune,0,10) | |
}else{ | |
str = append(str,r) | |
} | |
} | |
res[string(str)]++ | |
return res | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used unicode.IsSpace instead. Still don't like the fact that a new rune array/slice must be allocated every time a token ends, since slices can't use negative indexes i guess this is the only option...