Documentation
¶
Overview ¶
Package consume2 provides ways to consume go values using generics.
Index ¶
- func ComposeFilters[T any](filters ...func(T) bool) func(T) bool
- type Consumer
- func AppendPtrsTo[T any](aSlicePtr *[]*T) Consumer[T]
- func AppendTo[T any](aSlicePtr *[]T) Consumer[T]
- func Compose[T any](consumers ...Consumer[T]) Consumer[T]
- func Filter[T any](consumer Consumer[T], filter func(value T) bool) Consumer[T]
- func Filterp[T any](consumer Consumer[T], filter func(ptr *T) bool) Consumer[T]
- func Map[T, U any](consumer Consumer[U], mapper func(T) U) Consumer[T]
- func MaybeMap[T, U any](consumer Consumer[U], mapper func(T) (U, bool)) Consumer[T]
- func Nil[T any]() Consumer[T]
- func Slice[T any](consumer Consumer[T], start, end int) Consumer[T]
- func TakeWhile[T any](consumer Consumer[T], filter func(value T) bool) Consumer[T]
- type ConsumerFunc
- type PageBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ComposeFilters ¶
ComposeFilters[T] returns a single function that filters T values by ANDing together all the filter functions passed in. The returned filter function applies the first function in filters then the second and so forth. If a function in filters returns false, then the functions after it are not evaluated. The returned filter function returns true for a value only if all the functions in filters return true for that value.
Types ¶
type Consumer ¶
type Consumer[T any] interface { // CanConsume returns true if this instance can consume a value. // Once CanConsume returns false, it should always return false. CanConsume() bool // Consume consumes a value. If CanConsume returns false, calling Consume // does not consume the value. Consume(value T) }
Consumer[T] consumes values of type T.
func AppendPtrsTo ¶
AppendPtrsTo[T] returns a Consumer[T] that appends pointers to values to the slice pointed to by aSlicePtr. The CanConsume method of returned consumer always returns true.
func AppendTo ¶
AppendTo[T] returns a Consumer[T] that appends values to the slice pointed to by aSlicePtr. The CanConsume method of returned consumer always returns true.
func Compose ¶
Compose[T] returns all the Consumer[T] values passed to it as a single Consumer[T]. When returned consumer consumes a value, all the passed in consumers consume that same value. The CanConsume method of returned consumer returns false when the CanConsume method of all the passed in consumers returns false.
func Filter ¶
Filter[T] returns a Consumer[T] that passes only the values for which the filter function returns true onto the underlying consumer.
func Filterp ¶
Filterp[T] works like Filter[T] except that the filter function accepts *T instead of T. If the filter function mutates the T value via the pointer passed to it and returns true, the mutated value is sent to the underlying consumer.
func Map ¶
Map[T,U] returns a Consumer[T] that applies a mapper function to the T value being consumed and sends the resulting U value to the underlying consumer.
func MaybeMap ¶
MaybeMap[T,U] works like Map[T,U] except that the mapper function can return false for a T value in which case no corresponding U value is sent to the underlying consumer.
func Nil ¶
Nil[T] returns a Consumer[T] that consumes no T values. The CanConsume() method always returns false and the Consume() method does nothing.
func Slice ¶
Slice[T] returns a Consumer[T] that passes the start th value consumed inclusive to the end th value consumed exclusive onto the underlying consumer where start and end are zero based. Note that if end <= start, the underlying consumer will never get any values. A negative start or end is treated as zero.
type ConsumerFunc ¶
type ConsumerFunc[T any] func(value T)
ConsumerFunc[T] makes any function accepting a T value implement Consumer[T]. CanConsume always returns true.
func (ConsumerFunc[T]) CanConsume ¶
func (c ConsumerFunc[T]) CanConsume() bool
CanConsume always returns true.
func (ConsumerFunc[T]) Consume ¶
func (c ConsumerFunc[T]) Consume(value T)
Consume invokes c, this function.
type PageBuilder ¶
type PageBuilder[T any] struct { // contains filtered or unexported fields }
PageBuilder[T] is a Consumer[T] that builds a specific page of T values. It consumes just enough T values needed to build the desired page.
func NewPageBuilder ¶
func NewPageBuilder[T any]( zeroBasedPageNo int, valuesPerPage int) *PageBuilder[T]
NewPageBuilder[T] creates a PageBuilder[T]. NewPageBuilder[T] panics if zeroBasedPageNo is negative, or if valuesPerPage <= 0.
func (*PageBuilder[T]) Build ¶
func (p *PageBuilder[T]) Build() (values []T, morePages bool)
Build builds the desired page of T values. morePages is true if there are more pages after the desired page. Build is called after this builder has consumed its T values.
func (*PageBuilder[T]) CanConsume ¶
func (p *PageBuilder[T]) CanConsume() bool
CanConsume returns false when this builder has all the T values it needs to build the desired page.
func (*PageBuilder[T]) Consume ¶
func (p *PageBuilder[T]) Consume(value T)
Consume consumes a single T value.