func main() { var est estimation.Estimator est = new(estimation.TwomomentsChan) var stdin *bufio.Scanner stdin = bufio.NewScanner(os.Stdin) log.Print("Starting to read\n") //setup file reader inchan := make(chan float64) go utils.CatFloat(stdin, inchan) //output print server outchan := make(chan string) go utils.Print(os.Stdout, outchan) //main loop var number float64 var ok bool for { select { case number, ok = <-inchan: if ok { est.Push(number) } } if !ok { //the channel has closed no new data is coming break } outchan <- est.String() } }
//Spawn: Start a loop where we read off the in channel update the state and //output the estimates on the outchan. func Spawn(est estimation.Estimator, inchan chan float64, outchan chan string) { var number float64 var ok bool for { select { case number, ok = <-inchan: if ok { est.Push(number) } } if !ok { //the channel has closed no new data is coming break } outchan <- est.String() } }
//TestPeriodicQueryMain: Run a test showing consume a stream of numbers, //estimating the distribution and and printing out the estimate at regular intervals of time. func TestPeriodicQueryMain(t *testing.T) { var est estimation.Estimator est = new(estimation.Twomoments) inchan, err := getInput(datafile) if err != nil { t.Error(err.Error()) } timedch := make(chan string) go utils.PeriodicQuery(time.Nanosecond, func() string { return est.String() }, timedch) outchan := make(chan string) go utils.Print(os.Stdout, outchan) var number float64 var ok bool var report string for { select { case number, ok = <-inchan: if ok { est.Push(number) } case report, ok = <-timedch: if ok { outchan <- report } if !ok { t.Logf("not ok in report\n") } } if !ok { break } } }