add Generate function

This commit is contained in:
faiface
2017-07-25 23:44:44 +02:00
parent 552196eb06
commit f3cbfa5503

View File

@@ -31,3 +31,26 @@ func Callback(f func()) Streamer {
return 0, false
})
}
// Generate returns a Streamer which successively streams Streamers obtains by calling the provided
// g function. The streaming stops when g returns nil.
func Generate(g func() Streamer) Streamer {
s := g()
return StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
if s == nil {
return 0, false
}
for len(samples) > 0 {
if s == nil {
break
}
sn, sok := s.Stream(samples)
if !sok {
s = g()
}
samples = samples[sn:]
n += sn
}
return n, true
})
}