-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordcount.go
51 lines (45 loc) · 964 Bytes
/
wordcount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"log"
"strconv"
"strings"
"unicode"
)
func main() {
var c client
if err := mapreduce.Satart(c); err != nil {
log.Fatalf("%v", err)
}
}
// Map and Reduce functions for a basic wordcount client
type Client struct{}
func (c Client) Map(key, value string, output chan<- mapreduce.Pair) error {
defer close(output)
lst := strings.Fields(value)
for _, elt := range lst {
word := strings.Map(func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
return unicode.ToLower(r)
}
return -1
}, elt)
}
if len(word) > 0 {
output <- mapreduce.Pair{Key: word, Value: "1"}
}
return nil
}
func (c Client) Reduce(key string, values <-chan string, output chan<- mapreduce.Pair) error {
defer close(output)
count := 0
for v := range values {
i, err := strconv.Atoi(v)
if err != nil {
return err
}
count += 1
}
p := mapreduce.Pair{Key: key, Value: strconv.Itoa(count)}
output <- p
return nil
}