consume

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2021 License: BSD-3-Clause Imports: 1 Imported by: 26

README

consume

Consumers written in Go.

Using

import "github.com/keep94/consume"

Documentation

Overview

Package consume provides useful ways to consume values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustCanConsume

func MustCanConsume(c Consumer)

MustCanConsume panics if c cannot consume

Types

type ConsumeFinalizer

type ConsumeFinalizer interface {
	Consumer

	// Caller calls Finalize after it is done passing values to this consumer.
	// Once caller calls Finalize(), CanConsume() returns false and Consume()
	// panics. Calls to Finalize are idempotent.
	Finalize()
}

ConsumeFinalizer adds a Finalize method to Consumer.

func AppendToSaveMemory added in v0.5.0

func AppendToSaveMemory(aValueSlicePointer interface{}) ConsumeFinalizer

AppendToSaveMemory works like AppendTo but saves on allocs. AppendTo does O(N) allocs where N is the number of items being appended. AppendToSaveMemory does only O(log N) worst case. It accomplishes this by not modifying the dimensions of the slice with every append. Because of this, caller must call Finalize() on the returned consumer when appending is finished so that it can adjust the dimensions of the slice one last time to fit the items appended. Not calling Finalize() results in a slice that has extra elements at the end.

func Page

func Page(
	zeroBasedPageNo int,
	itemsPerPage int,
	aValueSlicePointer interface{},
	morePages *bool) ConsumeFinalizer

Page returns a consumer that does pagination. The items in page fetched get stored in the slice pointed to by aValueSlicePointer. Note that aValueSlicePointer is a pointer to a slice of values that support assignment. If there are more pages after page fetched, Page sets morePages to true; otherwise, it sets morePages to false. Note that the values stored at aValueSlicePointer and morePages are undefined until caller calls Finalize() on returned ConsumeFinalizer. Page panics if zeroBasedPageNo is negative, if itemsPerPage <= 0, or if aValueSlicePointer is not a pointer to a slice.

type Consumer

type Consumer interface {

	// CanConsume returns true if this instance can consume a value.
	// Once CanConsume returns false, it should always return false.
	CanConsume() bool

	// Consume consumes the value that ptr points to. Consume panics if
	// CanConsume() returns false.
	Consume(ptr interface{})
}

Consumer consumes values.

func AppendPtrsTo

func AppendPtrsTo(aPointerSlicePointer interface{}) Consumer

AppendPtrsTo returns a Consumer that appends consumed values to the slice pointed to by aPointerSlicePointer. Each time the returned Consumer consumes a value, it allocates a new value on the heap, copies the consumed value to that allocated value, and finally appends the pointer to the newly allocated value to the slice pointed to by aPointerSlicePointer. aPointerSlicePointer is a pointer to a slice of pointers to values supporting assignment. The CanConsume method of returned consumer always returns true.

func AppendTo

func AppendTo(aValueSlicePointer interface{}) Consumer

AppendTo returns a Consumer that appends consumed values to the slice pointed to by aValueSlicePointer. aValueSlicePointer is a pointer to a slice of values supporting assignment. The CanConsume method of returned consumer always returns true.

func Compose

func Compose(consumers ...Consumer) Consumer

Compose returns the consumers passed to it as a single Consumer. When returned consumer consumes a value, each consumer passed in that is able to consume a value consumes that value. CanConsume() of returned consumer returns false when the CanConsume() method of each consumer passed in returns false.

func MapFilter

func MapFilter(consumer Consumer, funcs ...interface{}) Consumer

MapFilter returns a Consumer that passes only filtered and mapped values onto the consumer parameter. The returned Consumer applies each function in funcs to the value passed to its Consume method. The resulting value is then passed to the Consume method of the consumer parameter. Each function in func returns a bool and takes one or two pointer arguments to values. If a function returns false, it means that the value passed to it should not be consumed. The one argument functions never change the value passed to it as they are simple filters. The 2 argument functions are mappers. They leave their first argument unchanged, but use it to set their second argument. This second argument is what gets passed to the next function in funcs or to consumer if it is the last function in funcs.

MapFilter can also take Mapper or Filterer interfaces instead of raw functions. These interfaces exist because calling a raw function via reflection costs two allocations for each value consumed.

The NewMapFilterer function can return a MapFilterer which represents zero or more of these functions chained together. MapFilterer instances can be passed as parameters to MapFilter just like the functions mentioned above. The MapFilter function takes a copy of any passed in MapFilterer instance, so any passed MapFilterer instance can safely be used at the same time as the returned Consumer.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/keep94/consume"
)

func main() {
	var evens []string
	consumer := consume.MapFilter(
		consume.AppendTo(&evens),
		func(ptr *int) bool {
			return (*ptr)%2 == 0
		},
		func(src *int, dest *string) bool {
			*dest = strconv.Itoa(*src)
			return true
		},
	)
	ints := []int{1, 2, 4}
	for _, i := range ints {
		if consumer.CanConsume() {
			consumer.Consume(&i)
		}
	}
	fmt.Println(evens)
}
Output:

[2 4]

func Nil

func Nil() Consumer

Nil returns a consumer that consumes nothing. Calling CanConsume() on returned consumer returns false, and calling Consume() on returned consumer panics.

func Slice

func Slice(consumer Consumer, start, end int) Consumer

Slice returns a Consumer that passes the start th value consumed inclusive to the end th value consumed exclusive onto consumer where start and end are zero based. The returned consumer ignores the first start values it consumes. After that it passes the values it consumes onto consumer until it has consumed end values. The CanConsume() method of returned consumer returns false if the CanConsume() method of the underlying consumer returns false or if the returned consumer has consumed end values. Note that if end <= start, the underlying consumer will never get any values. A negative start or end is treated as 0.

func TakeWhile added in v0.4.0

func TakeWhile(consumer Consumer, funcs ...interface{}) Consumer

TakeWhile works like MapFilter except that returned Consumer only accepts values until one is filtered out. Once returned Consumer filters out a value, its CanConsume() method always returns false.

type ConsumerFunc

type ConsumerFunc func(ptr interface{})

The ConsumerFunc type is an adapter to allow the use of an ordinary function as a Consumer. ConsumerFunc can always consume.

func (ConsumerFunc) CanConsume

func (c ConsumerFunc) CanConsume() bool

CanConsume always returns true.

func (ConsumerFunc) Consume

func (c ConsumerFunc) Consume(ptr interface{})

Consume invokes c, this function.

type Filterer added in v0.5.0

type Filterer interface {

	// Filter returns true if the value ptr points to should be included
	// or false otherwise.
	Filter(ptr interface{}) bool
}

Filterer filters a value

type MapFilterer

type MapFilterer interface {

	// MapFilter applies the chained filter and map functions to what ptr
	// points to while leaving it unchanged. MapFilter returns nil if ptr
	// should be filtered out; returns ptr itself; or returns a pointer to
	// a mapped value. If MapFilter returns a pointer to a mapped value,
	// it gets overwritten with each call to MapFilter because such values
	// are stored within this MapFilterer to avoid memory allocations.
	MapFilter(ptr interface{}) interface{}
	// contains filtered or unexported methods
}

MapFilterer represents zero or more functions like the ones passed to MapFilter chained together.

func NewMapFilterer

func NewMapFilterer(funcs ...interface{}) MapFilterer

NewMapFilterer creates a MapFilterer from multiple functions like the ones passed to MapFilter chained together. The returned MapFilterer can be passed as a parameter to MapFilter or to NewMapFilterer. The returned MapFilterer contains copies of any MapFilterers passed in. Therefore, the returned MapFilterer and any passed in MapFilterers can safely be used at the same time.

type Mapper added in v0.5.0

type Mapper interface {

	// Map takes a pointer to a value and returns a pointer to the
	// mapped value. Map returns the same pointer every time, but the
	// value that pointer points to changes.
	Map(ptr interface{}) interface{}

	// Clone clones this Mapper. The Map method of the cloned Mapper
	// returns a different pointer from the original.
	Clone() Mapper
}

Mapper maps a value to a new value.

Jump to

Keyboard shortcuts

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