コード例 #1
0
ファイル: demo.go プロジェクト: luffyhwl/gopark
	gopark.RegisterFunc(reducer_func)
}

var map_func = gopark.NewMapperFunc("map_func",
	func(_ interface{}) interface{} {
		x := rand.Float32()
		y := rand.Float32()
		if x*x+y*y < 1 {
			return 1
		} else {
			return 0
		}
	})

var reducer_func = gopark.NewReducerFunc("reducer_func",
	func(x, y interface{}) interface{} {
		return x.(int) + y.(int)
	})

func main() {
	go func() {
		var m runtime.MemStats
		for {
			runtime.ReadMemStats(&m)
			fmt.Printf("MemStats: %d,%d,%d,%d\n", m.HeapSys, m.HeapAlloc,
				m.HeapIdle, m.HeapReleased)
			time.Sleep(1 * time.Second)
		}
	}()

	go func() {
		log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
コード例 #2
0
ファイル: wc.go プロジェクト: luffyhwl/gopark
var (
	fn = gopark.NewFlatMapperFunc("fn", func(x interface{}) []interface{} {
		vals := strings.Fields(x.(string))
		results := make([]interface{}, len(vals))
		for idx, v := range vals {
			results[idx] = v
		}
		return results
	})
	fnMap = gopark.NewMapperFunc("fnMap", func(x interface{}) interface{} {
		return &gopark.KeyValue{x, 1}
	})

	fnReduce = gopark.NewReducerFunc("fnReduce", func(x, y interface{}) interface{} {
		return x.(int) + y.(int)
	})
)

func init() {
	gopark.RegisterFunc(fn)
	gopark.RegisterFunc(fnMap)
	gopark.RegisterFunc(fnReduce)
}
func WC() {
	ctx := gopark.NewContext("gopark")
	defer ctx.Stop()
	file := ctx.TextFile("wc.go")
	words := file.FlatMap(fn).Map(fnMap)

	wc := words.ReduceByKey(fnReduce).CollectAsMap()