Documentation
¶
Index ¶
- func BatchChan[T any](items <-chan T, size int, to chan []T)
- func BatchMap[K comparable, V any](items map[K]V, size int) []map[K]V
- func BatchSlice[T any](items []T, size int) [][]T
- func ChannelIntoSlice[T any](ch chan T, out []T) []T
- func ChannelToSlice[T any](ch chan T) (out []T)
- func CloseMany[T any](channels ...chan T)
- func CloseManyWriters[T any](channels ...chan<- T)
- func Deduplicate[V comparable](values []V) []V
- func ExtractFirst[T any](values []T, fn func(T) bool) (v T, exists bool)
- func ExtractToChannel[T, V any](in []T, fn func(T) (V, bool), out chan V)
- func ExtractToSlice[T, V any](in []T, fn func(T) (V, bool), out []V) []V
- func FanIn[T any](to chan<- T, from ...<-chan T)
- func FanInAndClose[T any](to chan<- T, from ...<-chan T)
- func FanOut[T any](from <-chan T, to ...chan<- T)
- func FanOutAndClose[T any](from <-chan T, to ...chan<- T)
- func FilterMap[K comparable, V any](in map[K]V, fn func(k K, v V) bool) map[K]V
- func FilterMapInplace[K comparable, V any](in map[K]V, fn func(k K, v V) bool)
- func FilterSlice[V any](in []V, fn func(v V) bool) []V
- func FilterSliceInplace[V any](in []V, fn func(v V) bool) []V
- func FilterSliceInto[V any](in, out []V, fn func(v V) bool) []V
- func IncrementalBatchMap[K comparable, V any](items map[K]V, batchSize int, k K, v V) (batch map[K]V)
- func IncrementalBatchSlice[T any](items []T, batchSize int, v T) (remaining, batch []T)
- func IncrementalSegmentMap[K comparable, V any, S comparable](segments map[S]map[K]V, k K, v V, f SegmentFuncKV[K, V, S])
- func IncrementalSegmentSlice[T any, S comparable](segments map[S][]T, item T, f SegmentFunc[T, S])
- func LoadChannel[T any](ch chan<- T, items ...T)
- func RoundRobin[T any](from <-chan T, to ...chan<- T)
- func SegmentChan[T any, S comparable](items <-chan T, f SegmentFunc[T, S]) map[S][]T
- func SegmentMap[K comparable, V any, S comparable](items map[K]V, f SegmentFuncKV[K, V, S]) map[S]map[K]V
- func SegmentSlice[T any, S comparable](items []T, f SegmentFunc[T, S]) map[S][]T
- func Transform[T, V any](values []T, fn func(t T) V) []V
- func TransformAndFilter[T, V any](values []T, fn func(t T) (V, bool)) []V
- func WorkerPoolFromChan[T any](ctx context.Context, items <-chan T, nWorkers int, f Job[T]) []error
- func WorkerPoolFromMap[K comparable, V any](ctx context.Context, items map[K]V, nWorkers int, f JobKV[K, V]) []error
- func WorkerPoolFromSlice[T any](ctx context.Context, items []T, nWorkers int, f Job[T]) []error
- type Counter
- type Deduplicator
- type Job
- type JobKV
- type KeyValue
- type ObjectCounter
- type ObjectDeduplicator
- type SegmentFunc
- type SegmentFuncKV
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BatchChan ¶
BatchChan reads from a channel and pushes batches of size `size` onto the `to` channel
func BatchMap ¶
func BatchMap[K comparable, V any](items map[K]V, size int) []map[K]V
BatchMap takes a map and breaks it up into sub-maps of `size` keys each
func BatchSlice ¶
BatchSlice takes a slice and breaks it up into sub-slices of `size` length each
func ChannelIntoSlice ¶
func ChannelIntoSlice[T any](ch chan T, out []T) []T
ChannelIntoSlice reads elements from the channel and returns appends them to the `out` slice. This operation will block until the channel is closed
func ChannelToSlice ¶
func ChannelToSlice[T any](ch chan T) (out []T)
ChannelToSlice reads elements from the channel and returns them as a slice. This operation will block until the channel is closed
func CloseMany ¶
func CloseMany[T any](channels ...chan T)
CloseMany closes all of the given channels
func CloseManyWriters ¶
func CloseManyWriters[T any](channels ...chan<- T)
CloseManyWriters closes all of the given write-only channels
func Deduplicate ¶ added in v0.1.6
func Deduplicate[V comparable](values []V) []V
Deduplicate returns a newly allocated slice without deplicate values
func ExtractFirst ¶ added in v1.2.0
ExtractFirst returns the first element in the slice for which fn(element) == true. If no matches are found, the second return argument is false.
func ExtractToChannel ¶ added in v1.2.0
ExtractToChannel calls the func `fn` for each element in `in` and pushes the result to `out` only if the second return argument of `fn` is true.
func ExtractToSlice ¶ added in v1.2.0
ExtractToSlice calls the func `fn` for each element in `in` and appends the result to `out` only if the second return argument of `fn` is true.
func FanIn ¶
func FanIn[T any](to chan<- T, from ...<-chan T)
FanIn reads from each `from` channel and writes to the `to` channel
func FanInAndClose ¶
func FanInAndClose[T any](to chan<- T, from ...<-chan T)
FanInAndClose reads from each `from` channel and writes to the `to` channel It closes the `to` channel once all messages are drained from the `from` channels.
func FanOut ¶
func FanOut[T any](from <-chan T, to ...chan<- T)
FanOut reads from the `from` channel and publishes the data across all `to` channels
func FanOutAndClose ¶
func FanOutAndClose[T any](from <-chan T, to ...chan<- T)
FanOutAndClose reads from the `from` channel and publishes the data across all `to` channels It closes each `to` channel once all messages are drained from the `from` channels.
func FilterMap ¶ added in v1.2.0
func FilterMap[K comparable, V any](in map[K]V, fn func(k K, v V) bool) map[K]V
FilterMap filters the input map with the function `fn` and return a new map The function `fn` accepts a key, value pair and should return true to keep the pair in the map
func FilterMapInplace ¶ added in v1.2.0
func FilterMapInplace[K comparable, V any](in map[K]V, fn func(k K, v V) bool)
FilterMapInplace filters the input map with the function `fn` in-place The function `fn` accepts a key, value pair and should return true to keep the pair in the map
func FilterSlice ¶ added in v1.2.0
FilterSlice filters the input slice with the function `fn` and return a new slice The function `fn` accepts a value and should return true to copy the value into the new slice
func FilterSliceInplace ¶ added in v1.2.0
FilterSliceInplace filters the input slice with the function `fn` in-place The function `fn` accepts a value and should return true to keep the value in the slice
func FilterSliceInto ¶ added in v1.2.0
FilterSliceInto filters the input slice `in` with the function `fn` into `out` The function `fn` accepts a value and should return true to copy the value into the `out` slice
func IncrementalBatchMap ¶
func IncrementalBatchMap[K comparable, V any](items map[K]V, batchSize int, k K, v V) (batch map[K]V)
IncrementalBatchMap incrementally builds map batches of size `batchSize` by adding elements to a map If the map is larger than `batchSize` elements, a single batch is returned along with the remaining elements of the map. Batched items are chosen by iterating the (unordered) map and thus you cannot make assumptions on which keys will exist in the batch. To avoid errors on the caller side, passing a batchSize < 1 will result in a batchSize of 1.
func IncrementalBatchSlice ¶
IncrementalBatchSlice incrementally builds slice batches of size `batchSize` by appending to a slice If the slice is larger than `batchSize` elements, a single batch is returned. The remaining elements of the slice are always returned. Batched items are returned from the head of the slice. To avoid errors on the caller side, passing a batchSize < 1 will result in a batchSize of 1.
func IncrementalSegmentMap ¶ added in v1.1.0
func IncrementalSegmentMap[K comparable, V any, S comparable](segments map[S]map[K]V, k K, v V, f SegmentFuncKV[K, V, S])
IncrementalSegmentMap adds the (key,value) pair to the correct segment inside `segments by calling `f` on `(k, v)`
func IncrementalSegmentSlice ¶ added in v1.1.0
func IncrementalSegmentSlice[T any, S comparable](segments map[S][]T, item T, f SegmentFunc[T, S])
IncrementalSegmentSlice adds the item to the correct segment inside `segments by calling `f` on `item`
func LoadChannel ¶
func LoadChannel[T any](ch chan<- T, items ...T)
LoadChannel puts all elements from `items` onto the channel `ch` This operation will block if not all items fit within the channel buffer or if there is not simultaneously another go routine reading from the channel.
func RoundRobin ¶
func RoundRobin[T any](from <-chan T, to ...chan<- T)
RoundRobin reads from the `from` channel and distributes the values in a round-robin fashion to the `to` channels.
func SegmentChan ¶
func SegmentChan[T any, S comparable](items <-chan T, f SegmentFunc[T, S]) map[S][]T
SegmentChan takes a channel and breaks it into smaller segmented slices using the provided function `f` The segments are returned in a map where the segment is the key.
func SegmentMap ¶
func SegmentMap[K comparable, V any, S comparable](items map[K]V, f SegmentFuncKV[K, V, S]) map[S]map[K]V
SegmentMap takes a map and breaks it into smaller segmented maps using the provided function `f` The segments are returned in a map where the segment is the key.
func SegmentSlice ¶
func SegmentSlice[T any, S comparable](items []T, f SegmentFunc[T, S]) map[S][]T
SegmentSlice takes a slice and breaks it into smaller segmented slices using the provided function `f` The segments are returned in a map where the segment is the key.
func Transform ¶ added in v1.2.0
func Transform[T, V any](values []T, fn func(t T) V) []V
Transform applies a transformation function to each element in the input slice and returns a new slice
func TransformAndFilter ¶ added in v1.2.0
TransformAndFilter applies a transformation function to each element in the input slice. If the second return value of the transformation function is false, then the value will be omitted from the output.
func WorkerPoolFromChan ¶
WorkerPoolFromChan starts a worker pool of size `nWorkers` and calls the function `f` for each element in the `items` channel
func WorkerPoolFromMap ¶
func WorkerPoolFromMap[K comparable, V any](ctx context.Context, items map[K]V, nWorkers int, f JobKV[K, V]) []error
WorkerPoolFromMap starts a worker pool of size `nWorkers` and calls the function `f` for each element in the `items` map
Types ¶
type Counter ¶ added in v0.1.7
type Counter[T comparable] struct { // contains filtered or unexported fields }
Counter is an entity that keeps track of the number items it encounters
func NewCounter ¶ added in v0.1.7
func NewCounter[T comparable]() *Counter[T]
NewCounter returns a new Counter which can be used to deduplicate slices values
func (*Counter[T]) Add ¶ added in v0.1.7
Add adds a item to the Counter and returns the current number of occurrences
func (*Counter[T]) AddMany ¶ added in v0.1.7
func (c *Counter[T]) AddMany(values []T)
AddMany adds all the values in the provided slice to the counter
type Deduplicator ¶ added in v0.1.6
type Deduplicator[T comparable] struct { // contains filtered or unexported fields }
Deduplicator is an entity that keeps track of items it has seen before so that it can deduplicate values
func NewDeduplicator ¶ added in v0.1.6
func NewDeduplicator[T comparable]() *Deduplicator[T]
NewDeduplicator returns a new Deduplicator which can be used to deduplicate slices values
func (*Deduplicator[T]) Add ¶ added in v0.1.6
func (dd *Deduplicator[T]) Add(v T) bool
Add adds a item to the Deduplicator and returns true if it was a new value (ie not a duplicate)
func (*Deduplicator[T]) Deduplicate ¶ added in v0.1.6
func (dd *Deduplicator[T]) Deduplicate(values []T) []T
Deduplicate returns a newly allocated slice without duplicate values by comparing it against values previously seen by the Deduplicator{}
func (*Deduplicator[T]) DeduplicateIndices ¶ added in v0.1.6
func (dd *Deduplicator[T]) DeduplicateIndices(values []T) []int
DeduplicateIndices returns the indices of values in the provided slice which are duplicates
func (*Deduplicator[T]) Reset ¶ added in v0.1.6
func (dd *Deduplicator[T]) Reset()
Reset removes any memory of duplicate values seen by this Deduplicator{}
func (*Deduplicator[T]) Seen ¶ added in v0.1.6
func (dd *Deduplicator[T]) Seen(v T) bool
Seen returns true if the provided value has already been added to the Deduplicator
type JobKV ¶
type JobKV[K comparable, V any] func(context.Context, K, V) error
JobKV is a function that the map worker pool executes
type KeyValue ¶
type KeyValue[K comparable, V any] struct { Key K Value V }
KeyValue is a tuple of key, value
type ObjectCounter ¶ added in v0.1.7
type ObjectCounter[T any] struct { // contains filtered or unexported fields }
ObjectCounter is a counter that works on objects by creating an ID for each element. Objects with the same ID will be counted in the same bucket.
func NewObjectCounter ¶ added in v0.1.7
func NewObjectCounter[T any](toId func(T) string) *ObjectCounter[T]
NewObjectCounter creates a ObjectCounter that uses the provided function in order to create IDs for needing to be counted.
func (*ObjectCounter[T]) Add ¶ added in v0.1.7
func (c *ObjectCounter[T]) Add(v T) int
Add adds an object to the Counter and returns the current number of occurrences
func (*ObjectCounter[T]) AddMany ¶ added in v0.1.7
func (c *ObjectCounter[T]) AddMany(values []T)
AddMany adds all the values in the provided slice to the counter
func (*ObjectCounter[T]) Count ¶ added in v0.1.7
func (c *ObjectCounter[T]) Count(v T) int
Count returns the current number of occurrences for the given object
func (*ObjectCounter[T]) Reset ¶ added in v0.1.7
func (c *ObjectCounter[T]) Reset()
Reset clears the values in the ObjectCounter{}
type ObjectDeduplicator ¶ added in v0.1.6
type ObjectDeduplicator[T any] struct { // contains filtered or unexported fields }
ObjectDeduplicator is a deduplicator that works on objects by creating an ID for each element. Objects with the same ID will be deduplicated.
func NewObjectDeduplicator ¶ added in v0.1.6
func NewObjectDeduplicator[T any](toId func(T) string) *ObjectDeduplicator[T]
NewObjectDeduplicator creates a ObjectDeduplicator that uses the provided function in order to create IDs for needing to be deduplicated.
func (*ObjectDeduplicator[T]) Add ¶ added in v0.1.6
func (dd *ObjectDeduplicator[T]) Add(v T) bool
Add adds a item to the ObjectDeduplicator and returns true if it was a new value (ie not a duplicate)
func (*ObjectDeduplicator[T]) Deduplicate ¶ added in v0.1.6
func (dd *ObjectDeduplicator[T]) Deduplicate(values []T) []T
Deduplicate returns a newly allocated slice without duplicate values by comparing it against values previously seen by the ObjectDuplicator{}
func (*ObjectDeduplicator[T]) DeduplicateIndices ¶ added in v0.1.6
func (dd *ObjectDeduplicator[T]) DeduplicateIndices(values []T) []int
DeduplicateIndices returns the indices of values in the provided slice which are duplicates
func (*ObjectDeduplicator[T]) Reset ¶ added in v0.1.6
func (dd *ObjectDeduplicator[T]) Reset()
Reset removes any memory of duplicate values seen by this Deduplicator{}
func (*ObjectDeduplicator[T]) Seen ¶ added in v0.1.6
func (dd *ObjectDeduplicator[T]) Seen(v T) bool
Seen returns true if the provided value has already been added to the ObjectDeduplicator
type SegmentFunc ¶
type SegmentFunc[T any, S comparable] func(T) S
SegmentFunc is a function that determines how an item of type `T` is segmented into a segment of type `S`
type SegmentFuncKV ¶
type SegmentFuncKV[K comparable, V any, S comparable] func(K, V) S