Esempio n. 1
0
// ChkbookEntries returns a Stream 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 Stream. If caller does not exhaust returned
// Stream, it must call Close on it to free up resources.
func ChkbkEntries(conn *sqlite.Conn, acctId int) (functional.Stream, 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
	}
	rowStream := functional.ReadRows(CloserStmt{stmt})
	return functional.Filter(&BalanceFilterer{bal}, rowStream), nil
}
Esempio n. 2
0
// Filter creates a new ErrorReportingConsumer whose Consume method applies
// f to the Stream before passing it onto c.
func Filter(
	c ErrorReportingConsumer, f functional.Filterer) ErrorReportingConsumer {
	return Modify(
		c,
		func(s functional.Stream) functional.Stream {
			return functional.Filter(f, s)
		})
}
Esempio n. 3
0
// Power returns a Stream that emits the power set of items. Next of
// returned Stream emits to an []int that has same length as items.
func Power(items []int) functional.Stream {
	len := len(items)
	if len == 0 {
		return functional.Slice(ess, 0, 1)
	}
	return functional.Concat(
		Power(items[:len-1]),
		functional.Deferred(func() functional.Stream {
			return functional.Filter(
				appendFilterer(items[len-1]),
				Power(items[:len-1]))
		}))
}
Esempio n. 4
0
// Power returns a Stream that emits the power set of items. Next of
// returned Stream emits to an []int that has same length as items.
func Power(items []int) functional.Stream {
	return functional.NewGenerator(func(e functional.Emitter) {
		len := len(items)
		if len == 0 {
			ptr := e.EmitPtr()
			if ptr != nil {
				p := ptr.(*[]int)
				*p = (*p)[:0]
				e.Return(nil)
			}
			return
		}
		if functional.EmitAll(Power(items[:len-1]), e) != nil {
			return
		}
		functional.EmitAll(functional.Filter(appendFilterer(items[len-1]), Power(items[:len-1])), e)
	})
}