Skip to content

Commit 1aa4781

Browse files
committed
chore: add examples
1 parent 0041139 commit 1aa4781

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

examples/sum.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/kevwan/mapreduce"
8+
)
9+
10+
func main() {
11+
val, err := mapreduce.MapReduce(func(source chan<- interface{}) {
12+
for i := 0; i < 10; i++ {
13+
source <- i
14+
}
15+
}, func(item interface{}, writer mapreduce.Writer, cancel func(error)) {
16+
i := item.(int)
17+
writer.Write(i * i)
18+
}, func(pipe <-chan interface{}, writer mapreduce.Writer, cancel func(error)) {
19+
var sum int
20+
for i := range pipe {
21+
sum += i.(int)
22+
}
23+
writer.Write(sum)
24+
})
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
fmt.Println("result:", val)
29+
}

readme-cn.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,49 @@
5353

5454
很简单,goroutine 中监听一个全局的结束 channel 就行。
5555

56+
## 简单示例
57+
58+
并行求平方和(不要嫌弃示例简单,只是模拟并发)
59+
60+
```go
61+
package main
62+
63+
import (
64+
"fmt"
65+
"log"
66+
67+
"github.com/kevwan/mapreduce"
68+
)
69+
70+
func main() {
71+
val, err := mapreduce.MapReduce(func(source chan<- interface{}) {
72+
// generator
73+
for i := 0; i < 10; i++ {
74+
source <- i
75+
}
76+
}, func(item interface{}, writer mapreduce.Writer, cancel func(error)) {
77+
// mapper
78+
i := item.(int)
79+
writer.Write(i * i)
80+
}, func(pipe <-chan interface{}, writer mapreduce.Writer, cancel func(error)) {
81+
// reducer
82+
var sum int
83+
for i := range pipe {
84+
sum += i.(int)
85+
}
86+
writer.Write(sum)
87+
})
88+
if err != nil {
89+
log.Fatal(err)
90+
}
91+
fmt.Println("result:", val)
92+
}
93+
```
94+
5695
## 强烈推荐!
5796

5897
go-zero: [https://github.com/zeromicro/go-zero](https://github.com/zeromicro/go-zero)
5998

6099
## 欢迎 star!⭐
61100

62-
如果你正在使用或者觉得这个项目对你有帮助,请 **star** 支持,感谢!
101+
如果你正在使用或者觉得这个项目对你有帮助,请 **star** 支持,感谢!

readme.md

+39
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,45 @@ How can I terminate the process at any time?
3939

4040
It's simple, just listen to a global end channel or the given context in the goroutine.
4141

42+
## A simple example
43+
44+
Calculate the sum of squares, simulating the concurrency.
45+
46+
```go
47+
package main
48+
49+
import (
50+
"fmt"
51+
"log"
52+
53+
"github.com/kevwan/mapreduce"
54+
)
55+
56+
func main() {
57+
val, err := mapreduce.MapReduce(func(source chan<- interface{}) {
58+
// generator
59+
for i := 0; i < 10; i++ {
60+
source <- i
61+
}
62+
}, func(item interface{}, writer mapreduce.Writer, cancel func(error)) {
63+
// mapper
64+
i := item.(int)
65+
writer.Write(i * i)
66+
}, func(pipe <-chan interface{}, writer mapreduce.Writer, cancel func(error)) {
67+
// reducer
68+
var sum int
69+
for i := range pipe {
70+
sum += i.(int)
71+
}
72+
writer.Write(sum)
73+
})
74+
if err != nil {
75+
log.Fatal(err)
76+
}
77+
fmt.Println("result:", val)
78+
}
79+
```
80+
4281
## References
4382

4483
go-zero: [https://github.com/zeromicro/go-zero](https://github.com/zeromicro/go-zero)

0 commit comments

Comments
 (0)