package main

import (
	"fmt"
	"strings"
)

// 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
func Fibonaccit() func() int {
	a, b := 0, 1
	return func() int {
		a, b = b, a+b
		return a
	}
}

type intGen func() int

func (g intGen) Read(
	p []byte) (n int, err error) {
	next := g()
	s := fmt.Sprintf("%d\n", next)

	// TODO: incorrect if p is too small!
	return strings.NewReader(s).Read(p)
}

func main() {
	var f intGen = Fibonaccit()
	for i := 0; i < 20; i++ {
		b := make([]byte, 1)
		n, err := f.Read(b)
		fmt.Printf("%d bytes read: %q. err = %v\n", n, b, err)
	}
}
