// Consume fetches the values. s is a Stream of T. func (pb *PageBuffer) Consume(s functional.Stream) { defer s.Close() pb.page_no = 0 pb.idx = 0 pb.is_end = false pb.err = nil for pb.err == nil && !pb.isDesiredPageRead() { if pb.idx > 0 { pb.page_no++ } offset := pb.pageOffset(pb.page_no) pb.idx, pb.err = readStreamIntoSlice( s, pb.buffer.Slice(offset, offset+pb.pageLen), pb.addrFunc) } if pb.err == nil { pb.is_end = s.Next(pb.addrFunc(pb.buffer.Index(pb.pageOffset(pb.page_no+1)))) == functional.Done } else if pb.err == functional.Done { pb.is_end = true pb.err = nil if pb.page_no > 0 && pb.idx == 0 { pb.page_no-- pb.idx = pb.pageLen } } }
// Consume prints all Entry values that s emits to stdout. On error, prints // the error encountered and halts. func (p Printer) Consume(s functional.Stream) { var entry Entry err := s.Next(&entry) for ; err == nil; err = s.Next(&entry) { fmt.Println(&entry) } if err != functional.Done { fmt.Printf("Error happened: %v\n", err) s.Close() } }
func readStreamIntoSlice( s functional.Stream, aSlice reflect.Value, addrFunc func(reflect.Value) interface{}) (numRead int, err error) { l := aSlice.Len() for numRead = 0; numRead < l; numRead++ { err = s.Next(addrFunc(aSlice.Index(numRead))) if err != nil { break } } return }
// FirstOnly reads the first value from stream storing it in ptr. // FirstOnly closes the stream. // FirstOnly returns emptyError if no values were on stream. func FirstOnly(stream functional.Stream, emptyError error, ptr interface{}) (err error) { defer func() { closeError := stream.Close() if err == nil { err = closeError } }() err = stream.Next(ptr) if err == functional.Done { err = emptyError return } return }
// Consume computes the total of all income Entry values that s emits if // Income is true; otherwise, it computes the total of all expense Entry // values s emits. func (t *Totaler) Consume(s functional.Stream) { var entry Entry err := s.Next(&entry) for ; err == nil; err = s.Next(&entry) { if t.Income { if entry.Amount < 0 { t.Total -= entry.Amount } } else { if entry.Amount > 0 { t.Total += entry.Amount } } } if err != functional.Done { s.Close() } }
func (c *errorReportingConsumerForTesting) Consume(s functional.Stream) { var x int for s.Next(&x) != functional.Done { c.count++ } }