示例#1
0
// Emits 01234567891011121314151617... as a Stream of runes.
func AllDigits() functional.Generator {
	return functional.NewGenerator(
		func(e functional.Emitter) {
			for number := 0; ; number++ {
				for _, ch := range strconv.Itoa(number) {
					ptr := e.EmitPtr()
					if ptr == nil {
						return
					}
					*(ptr.(*rune)) = ch
				}
			}
		})
}
示例#2
0
// ChkbookEntries returns a Generator that emits all the entries in a
// checkbook ordered by most recent to least recent. conn is the sqlite
// connection; acctId is the id of the account for which to print entries.
// If acctId does not match a valid account, ChkbookEntries will return an
// error and nil for the Generator. If caller does not exhaust returned
// Generator, it must call Close on it to free up resources.
func ChkbkEntries(conn *sqlite.Conn, acctId int) (functional.Generator, error) {
	stmt, err := conn.Prepare("select balance from balances where acct_id = ?")
	if err != nil {
		return nil, err
	}
	if err = stmt.Exec(acctId); err != nil {
		stmt.Finalize()
		return nil, err
	}
	if !stmt.Next() {
		stmt.Finalize()
		return nil, errors.New("No balance")
	}
	var bal int64
	if err = stmt.Scan(&bal); err != nil {
		stmt.Finalize()
		return nil, err
	}
	stmt.Finalize()
	stmt, err = conn.Prepare("select date, name, amount from entries where acct_id = ? order by date desc")
	if err != nil {
		return nil, err
	}
	if err = stmt.Exec(acctId); err != nil {
		stmt.Finalize()
		return nil, err
	}
	return functional.NewGenerator(func(emitter functional.Emitter) {
		rowStream := functional.ReadRows(stmt)
		for ptr := emitter.EmitPtr(); ptr != nil && rowStream.Next(ptr); ptr = emitter.EmitPtr() {
			entry := ptr.(*Entry)
			entry.Balance = bal
			bal += entry.Amount
		}
		stmt.Finalize()
	}), nil
}