MCPcopy
hub / github.com/chrislusf/glow / init

Function init

examples/invert_file/invert_file.go:24–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

22)
23
24func 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
62func main() {
63 flag.Parse()

Callers

nothing calls this directly

Calls 5

MapMethod · 0.80
GroupByKeyMethod · 0.80
SourceMethod · 0.80
ReduceByKeyMethod · 0.80
TextFileMethod · 0.80

Tested by

no test coverage detected