From f3cbfa55032c00bc998f18c368224e5549bb7e4e Mon Sep 17 00:00:00 2001 From: faiface Date: Tue, 25 Jul 2017 23:44:44 +0200 Subject: [PATCH] add Generate function --- streamers.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/streamers.go b/streamers.go index e86cf7c..63ca801 100644 --- a/streamers.go +++ b/streamers.go @@ -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 + }) +}