consume2

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2023 License: BSD-3-Clause Imports: 0 Imported by: 35

README

consume2

Build pipelines that consume values using Go generics.

Using

import "github.com/keep94/consume2"

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsFunc added in v0.4.0

func AsFunc[T any](consumer Consumer[T]) func(T) bool

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

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.

func FromGenerator added in v0.5.0

func FromGenerator[T any](generator func() (T, bool), consumer Consumer[T])

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

func FromIntGenerator(generator func() int, consumer Consumer[int])

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

func FromPtrSlice[T any](aslice []*T, consumer Consumer[T])

FromPtrSlice sends values in aslice to consumer skipping nil pointers in aslice.

func FromSlice added in v0.5.0

func FromSlice[T any](aslice []T, consumer Consumer[T])

FromSlice sends values in aslice to consumer.

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.

type Pipeline added in v0.5.0

type Pipeline[T, U any] func(inner Consumer[U]) Consumer[T]

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

func Identity[T any]() Pipeline[T, T]

Identity returns a Pipeline that emits the same T values it receives.

func Join added in v0.5.0

func Join[T, U, V any](
	first Pipeline[T, U], second Pipeline[U, V]) Pipeline[T, V]

Join joins two pipelines into a single pipeline.

func PFilter added in v0.5.0

func PFilter[T any](filter func(value T) bool) Pipeline[T, T]

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

func PFilterp[T any](filter func(ptr *T) bool) Pipeline[T, T]

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

func PMap[T, U any](mapper func(T) U) Pipeline[T, U]

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

func PMaybeMap[T, U any](mapper func(T) (U, bool)) Pipeline[T, U]

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

func PSlice[T any](start, end int) Pipeline[T, T]

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

func PTakeWhile[T any](filter func(value T) bool) Pipeline[T, T]

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

func (p Pipeline[T, U]) AppendTo(aSlicePtr *[]U) Consumer[T]

AppendTo returns a Consumer that collects the T values for this pipeline and appends the U values this pipeline emits to aSlicePtr.

func (Pipeline[T, U]) Call added in v0.7.0

func (p Pipeline[T, U]) Call(f func(value U)) Consumer[T]

Call works like Run except that it calls f on each U value this pipeline emits.

func (Pipeline[T, U]) Run added in v0.5.0

func (p Pipeline[T, U]) Run(consumer Consumer[U]) Consumer[T]

Run returns a Consumer that collects the T values for this pipeline and sends the U values this pipeline emits to the consumer parameter.

Jump to

Keyboard shortcuts

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