consume2

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2022 License: BSD-3-Clause Imports: 0 Imported by: 36

README

consume2

Build pipelines that consume values using Go generics.

Using

import "github.com/keep94/consume2"

Documentation

Overview

Package consume2 provides ways to consume go values using generics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComposeFilters

func ComposeFilters[T any](filters ...func(T) bool) func(T) bool

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

func AppendPtrsTo[T any](aSlicePtr *[]*T) Consumer[T]

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

func AppendTo[T any](aSlicePtr *[]T) Consumer[T]

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

func Compose[T any](consumers ...Consumer[T]) Consumer[T]

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

func Filter[T any](
	consumer Consumer[T], filter func(value T) bool) Consumer[T]

Filter[T] returns a Consumer[T] that passes only the values for which the filter function returns true onto the underlying consumer.

func Filterp

func Filterp[T any](
	consumer Consumer[T], filter func(ptr *T) bool) Consumer[T]

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

func Map[T, U any](
	consumer Consumer[U], mapper func(T) U) Consumer[T]

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

func MaybeMap[T, U any](
	consumer Consumer[U], mapper func(T) (U, bool)) Consumer[T]

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

func Nil[T any]() Consumer[T]

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

func Slice[T any](consumer Consumer[T], start, end int) Consumer[T]

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.

func TakeWhile

func TakeWhile[T any](
	consumer Consumer[T], filter func(value T) bool) Consumer[T]

TakeWhile[T] works like Filter[T] except that returned consumer only accepts values until one is filtered out.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL