Documentation
¶
Overview ¶
Package consume2 builds pipelines that consume values using Go generics.
Example (Pipeline) ¶
package main import ( "fmt" "github.com/keep94/consume2" ) type Person struct { Name string Age int } func FirstNamesOver40(people []Person, n int) (result []string) { over40 := consume2.PFilter(func(p Person) bool { return p.Age >= 40 }) namesOver40 := consume2.Join( over40, consume2.PMap(func(p Person) string { return p.Name })) firstNamesOver40 := consume2.Join( namesOver40, consume2.PSlice[string](0, n)) consume2.FromSlice(people, firstNamesOver40.AppendTo(&result)) return } func main() { people := []Person{ {Name: "Alice", Age: 43}, {Name: "Bob", Age: 35}, {Name: "Charlie", Age: 62}, {Name: "David", Age: 40}, {Name: "Ellen", Age: 41}, } fmt.Println(FirstNamesOver40(people, 3)) }
Output: [Alice Charlie David]
Index ¶
- func AsFunc[T any](consumer Consumer[T]) func(T) bool
- func ComposeFilters[T any](filters ...func(T) bool) func(T) bool
- func FromGenerator[T any](generator func() (T, bool), consumer Consumer[T])
- func FromIntGenerator(generator func() int, consumer Consumer[int])
- func FromPtrSlice[T any](aslice []*T, consumer Consumer[T])
- func FromSlice[T any](aslice []T, consumer Consumer[T])
- 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
- type Pipeline
- func Identity[T any]() Pipeline[T, T]
- func Join[T, U, V any](first Pipeline[T, U], second Pipeline[U, V]) Pipeline[T, V]
- func PFilter[T any](filter func(value T) bool) Pipeline[T, T]
- func PFilterp[T any](filter func(ptr *T) bool) Pipeline[T, T]
- func PMap[T, U any](mapper func(T) U) Pipeline[T, U]
- func PMaybeMap[T, U any](mapper func(T) (U, bool)) Pipeline[T, U]
- func PSlice[T any](start, end int) Pipeline[T, T]
- func PTakeWhile[T any](filter func(value T) bool) Pipeline[T, T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsFunc ¶ added in v0.4.0
AsFunc converts a Consumer into a function that consumes its paramter and returns false when no more values can be consumed. AsFunc allows interoperability with other go packages such as github.com/google/btree.
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.
func FromGenerator ¶ added in v0.5.0
FromGenerator sends values from generator to consumer. generator returns false when there are no more values to send.
func FromIntGenerator ¶ added in v0.5.0
FromIntGenerator sends ints from generator to consumer. generator returns a negative number when there are no more ints to send.
func FromPtrSlice ¶ added in v0.5.0
FromPtrSlice sends values in aslice to consumer skipping nil pointers in aslice.
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.
type Pipeline ¶ added in v0.5.0
Pipeline is an abstraction that emits a group of U values from a group of T values. The returned Consumer consumes the T values and sends the U values to inner.
func Identity ¶ added in v0.6.0
Identity returns a Pipeline that emits the same T values it receives.
func PFilter ¶ added in v0.5.0
PFilter returns a Pipeline that applies filter to the T values it receives and emits only those T values for which filter returns true.
func PFilterp ¶ added in v0.5.0
PFilterp is like PFilter except that the returned pipeline can mutate the T values it emits while leaving the original T values the same.
func PMap ¶ added in v0.5.0
PMap returns a Pipeline that applies mapper to the T values it receives and emits the resulting U values.
func PMaybeMap ¶ added in v0.5.0
PMaybeMap returns a Pipeline that applies mapper to the T values it receives and emits the resulting U values for which mapper returns true.
func PSlice ¶ added in v0.5.0
PSlice returns a Pipeline that emits the start T value it receives inclusive up to the end T value it receives exclusive. start and end are zero based.
func PTakeWhile ¶ added in v0.5.0
PTakeWhile returns a Pipeline that emits the first T values it receives for which filter returns true.
func (Pipeline[T, U]) AppendTo ¶ added in v0.5.0
AppendTo returns a Consumer that collects the T values for this pipeline and appends the U values this pipeline emits to aSlicePtr.