()
| 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | f1.Source(func(out chan WordSentence) { |
| 26 | bytes, err := ioutil.ReadFile(*fileName) |
| 27 | if err != nil { |
| 28 | println("Failed to read", *fileName) |
| 29 | return |
| 30 | } |
| 31 | lines := strings.Split(string(bytes), "\n") |
| 32 | for lineNumber, line := range lines { |
| 33 | for _, word := range strings.Split(line, " ") { |
| 34 | if word != "" { |
| 35 | out <- WordSentence{word, lineNumber} |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | }, 3).Map(func(ws WordSentence) (string, int) { |
| 40 | return ws.Word, ws.LineNumber |
| 41 | }).GroupByKey().Map(func(word string, lineNumbers []int) { |
| 42 | fmt.Printf("%s : %v\n", word, lineNumbers) |
| 43 | }) |
| 44 | |
| 45 | f2.TextFile( |
| 46 | "/etc/passwd", 2, |
| 47 | ).Map(func(line string, ch chan string) { |
| 48 | for _, token := range strings.Split(line, " ") { |
| 49 | ch <- token |
| 50 | } |
| 51 | }).Map(func(key string) (string, int) { |
| 52 | return key, 1 |
| 53 | }).ReduceByKey(func(x int, y int) int { |
| 54 | // println("reduce:", x+y) |
| 55 | return x + y |
| 56 | }).Map(func(key string, x int) { |
| 57 | println(key, ":", x) |
| 58 | }) |
| 59 | |
| 60 | } |
| 61 | |
| 62 | func main() { |
| 63 | flag.Parse() |
nothing calls this directly
no test coverage detected