pipe

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewYeti added in v1.0.5

func NewYeti() internalpipe.YeetSnag

NewYeti creates a brand new Yeti - an object for error handling.

func Reduce

func Reduce[SrcT, DstT any](p Piper[SrcT], fn func(*DstT, *SrcT) DstT, initVal ...DstT) DstT

Reduce applies reduce operation on Pipe of type SrcT and returns result of type DstT. initVal is an optional parameter to initialize a value that should be used on the first step of reduce.

Types

type Accum

type Accum[T any] func(*T, *T) T

Accum is a standard type for reduce function. It is guaranteed that arguments are not nil, so you can dereference them with no check.

func Acc

func Acc[T any](fn func(T, T) T) Accum[T]

Acc creates an Accum from a function of a different signature. Using it allows inner function `fn` not to use dereference, but also it leads to a larger memory allocation.

type Comparator

type Comparator[T any] func(*T, *T) bool

Comparator is a standard type for sorting comparisons. It is guaranteed that argument is not nil, so you can dereference it with no check.

func Comp

func Comp[T any](cmp func(T, T) bool) Comparator[T]

Comp creates a Comparator from a function of a different signature. Using it allows inner function `cmp` not to use dereference, but also it leads to a larger memory allocation.

type Pipe

type Pipe[T any] struct {
	internalpipe.Pipe[T]
}

Pipe implements the pipe on any slice. Pipe may be initialized with `Slice`, `Func`, `Cycle` or `Range`.

func (*Pipe[T]) Any

func (p *Pipe[T]) Any() *T

Any returns a pointer to a random element in the pipe or nil if none left.

func (*Pipe[T]) Count

func (p *Pipe[T]) Count() int

Count evaluates all the pipeline and returns the amount of items.

func (*Pipe[T]) Do

func (p *Pipe[T]) Do() []T

Do evaluates all the pipeline and returns the result slice.

func (*Pipe[T]) Entrails

func (p *Pipe[T]) Entrails() *internalpipe.Pipe[T]

Entrails is an out-of-Piper interface method to provide Map[T1 -> T2].

func (*Pipe[T]) Erase added in v1.0.5

func (p *Pipe[T]) Erase() Piper[any]

Erase wraps all pipe values to interface{} type, so you are able to use pipe methods with type convertions. You can use collectors from collectiors.go file of this package to collect results into a particular type.

func (*Pipe[T]) Filter

func (p *Pipe[T]) Filter(fn Predicate[T]) Piper[T]

Filter leaves only items with true predicate fn.

func (*Pipe[T]) First

func (p *Pipe[T]) First() *T

First returns the first element of the pipe.

func (*Pipe[T]) Map

func (p *Pipe[T]) Map(fn func(T) T) Piper[T]

Map applies given function to each element of the underlying slice returns the slice where each element is n[i] = f(p[i]).

func (*Pipe[T]) MapFilter added in v1.0.5

func (p *Pipe[T]) MapFilter(fn func(T) (T, bool)) Piper[T]

MapFilter applies given function to each element of the underlying slice, if the second returning value of fn is false, the element is skipped (may be useful for error handling). returns the slice where each element is n[i] = f(p[i]) if it is not skipped.

func (*Pipe[T]) Parallel

func (p *Pipe[T]) Parallel(n uint16) Piper[T]

Parallel set n - the amount of goroutines to run on. Only the first Parallel() in a pipe chain is applied.

func (*Pipe[T]) Promices added in v1.0.5

func (p *Pipe[T]) Promices() []func() (T, bool)

Promices returns an array of Promice values - functions to be evaluated to get the value on i'th place. Promice returns two values: the evaluated value and if it is not skipped.

func (*Pipe[T]) Reduce

func (p *Pipe[T]) Reduce(fn Accum[T]) *T

Reduce applies the result of a function to each element one-by-one: f(p[n], f(p[n-1], f(p[n-2, ...]))). It is recommended to use reducers from the default reducer if possible to decrease memory allocations.

func (*Pipe[T]) Snag added in v1.0.5

func (p *Pipe[T]) Snag(h func(error)) Piper[T]

Snag links an error handler to the previous Pipe method.

func (*Pipe[T]) Sort

func (p *Pipe[T]) Sort(less Comparator[T]) Piper[T]

Sort sorts the underlying slice on a current step of a pipeline.

func (*Pipe[T]) Sum

func (p *Pipe[T]) Sum(plus Accum[T]) T

Sum returns the sum of all elements. It is similar to Reduce but is able to work in parallel.

func (*Pipe[T]) Yeti added in v1.0.5

func (p *Pipe[T]) Yeti(y internalpipe.YeetSnag) Piper[T]

Yeti links a yeti error handler to the Pipe.

type PipeNL

type PipeNL[T any] struct {
	internalpipe.Pipe[T]
}

PipeNL implements the pipe on any slice. PipeNL may be initialized with `Slice`, `Func`, `Cycle` or `Range`.

func (*PipeNL[T]) Any

func (p *PipeNL[T]) Any() *T

Any returns a pointer to a random element in the pipe or nil if none left.

func (*PipeNL[T]) Entrails

func (p *PipeNL[T]) Entrails() *internalpipe.Pipe[T]

Entrails is an out of Piper interface method to provide Map[T1 -> T2].

func (*PipeNL[T]) Erase added in v1.0.5

func (p *PipeNL[T]) Erase() PiperNoLen[any]

Erase wraps all pipe values to interface{} type, so you are able to use pipe methods with type convertions. You can use collectors from collectiors.go file of this package to collect results into a particular type.

func (*PipeNL[T]) Filter

func (p *PipeNL[T]) Filter(fn Predicate[T]) PiperNoLen[T]

Filter leaves only items with true predicate fn.

func (*PipeNL[T]) First

func (p *PipeNL[T]) First() *T

First returns the first element of the pipe.

func (*PipeNL[T]) Gen

func (p *PipeNL[T]) Gen(n int) Piper[T]

Gen set the amount of values to generate as initial array. It's applied only the first Gen() or Take() function in the pipe.

func (*PipeNL[T]) Map

func (p *PipeNL[T]) Map(fn func(T) T) PiperNoLen[T]

Map applies given function to each element of the underlying slice returns the slice where each element is n[i] = f(p[i]).

func (*PipeNL[T]) MapFilter added in v1.0.5

func (p *PipeNL[T]) MapFilter(fn func(T) (T, bool)) PiperNoLen[T]

MapFilter applies given function to each element of the underlying slice, if the second returning value of fn is false, the element is skipped (may be useful for error handling). returns the slice where each element is n[i] = f(p[i]) if it is not skipped.

func (*PipeNL[T]) Parallel

func (p *PipeNL[T]) Parallel(n uint16) PiperNoLen[T]

Parallel set n - the amount of goroutines to run on. Only the first Parallel() in a pipe chain is applied.

func (*PipeNL[T]) Snag added in v1.0.5

func (p *PipeNL[T]) Snag(h func(error)) PiperNoLen[T]

Snag links an error handler to the previous Pipe method.

func (*PipeNL[T]) Take

func (p *PipeNL[T]) Take(n int) Piper[T]

Take is used to set the amount of values expected to be in result slice. It's applied only the first Gen() or Take() function in the pipe.

func (*PipeNL[T]) Yeti added in v1.0.5

func (p *PipeNL[T]) Yeti(y internalpipe.YeetSnag) PiperNoLen[T]

Yeti links a yeti error handler to the Pipe.

type Piper

type Piper[T any] interface {
	// contains filtered or unexported methods
}

Piper interface contains all methods of a pipe with determened length.

func Collect added in v1.0.5

func Collect[DstT any](p Piper[any]) Piper[DstT]

Collect translates Piper with erased type (achieved by calling an Erase method of any type).

func Map

func Map[SrcT any, DstT any](
	p Piper[SrcT],
	fn func(x SrcT) DstT,
) Piper[DstT]

Map applies function on a Piper of type SrcT and returns a Pipe of type DstT.

func MapFilter added in v1.0.6

func MapFilter[SrcT, DstT any](
	p Piper[SrcT],
	fn func(x SrcT) (DstT, bool),
) Piper[DstT]

MapFilter applies function on a Piper of type SrcT and returns a Pipe of type DstT. fn returns a value of DstT type and true if this value is not skipped.

func Range

func Range[T constraints.Integer | constraints.Float](start, finish, step T) Piper[T]

Range creates a lazy sequence of type 'T', which consists of values starting from 'start', incrementing by 'step' and stopping just before 'finish', i.e. [start..finish). The type 'T' can be either an integer or a float.

func Repeat added in v1.0.4

func Repeat[T any](x T, n int) Piper[T]

Repeat creates a lazy sequence of the objects like x with the length of n.

func Slice

func Slice[T any](dt []T) Piper[T]

Slice creates a Pipe from a slice

type PiperNoLen

type PiperNoLen[T any] interface {
	// contains filtered or unexported methods
}

PiperNoLen represents methods available to a Pipe type with no length determened.

func CollectNL added in v1.0.5

func CollectNL[DstT any](p PiperNoLen[any]) PiperNoLen[DstT]

CollectNL translates PiperNL with erased type (achieved by calling an Erase method of any type).

func Cycle

func Cycle[T any](a []T) PiperNoLen[T]

Cycle creates a lazy sequence that cycles through the elements of the provided slice 'a'. To use the resulting sequence, the 'Cycle' function returns a 'PiperNoLen' object.

Use the 'Take' or 'Gen' functions to set the number of output values to generate, or use the 'Until' function to enforce a limit based on a predicate function.

func Fn

func Fn[T any](fn func(i int) T) PiperNoLen[T]

Fu creates a lazy sequence by applying the provided function 'fn' to each index 'i', and setting the result to the sequence at index 'i'.

'fn' is a shortened version of the 'Func' function, where the second argument is true by default.

Use the 'Take' or 'Gen' functions to set the number of output values to generate, or use the 'Until' function to enforce a limit based on a predicate function.

func Func

func Func[T any](fn func(i int) (T, bool)) PiperNoLen[T]

Func creates a lazy sequence by applying the provided function 'fn' to each index 'i', placing the resulting object into the sequence at index 'i'.

The 'fn' function must return an object of type T, along with a boolean indicating if it exists.

Use the 'Take' or 'Gen' functions to set the number of output values to generate, or use the 'Until' function to enforce a limit based on a predicate function.

func FuncP

func FuncP[T any](fn func(i int) (*T, bool)) PiperNoLen[T]

FuncP is the same as Func but allows to return pointers to the values. It creates a lazy sequence by applying the provided function 'fn' to each index 'i', and setting the result to the sequence at index 'i' as a pointer to the object of type 'T'.

'fn' should return a pointer to an object of type 'T', along with a boolean indicating whether the object exists.

Use the 'Take' or 'Gen' functions to set the number of output values to generate, or use the 'Until' function to enforce a limit based on a predicate function.

func MapFilterNL added in v1.0.6

func MapFilterNL[SrcT, DstT any](
	p PiperNoLen[SrcT],
	fn func(x SrcT) (DstT, bool),
) PiperNoLen[DstT]

MapFilterNL applies function on a PiperNoLen of type SrcT and returns a Pipe of type DstT. fn returns a value of DstT type and true if this value is not skipped.

func MapNL

func MapNL[SrcT, DstT any](
	p PiperNoLen[SrcT],
	fn func(x SrcT) DstT,
) PiperNoLen[DstT]

MapNL applies function on a PiperNoLen of type SrcT and returns a Pipe of type DstT.

type Predicate

type Predicate[T any] func(*T) bool

Predicate is a standard type for filtering. It is guaranteed that argument is not nil, so you can dereference it with no check.

func Pred

func Pred[T any](fn func(T) bool) Predicate[T]

Pred creates a Predicate from a function of a different signature. Using it allows inner function `fn` not to use dereference, but also it leads to a larger memory allocation.

type Promice added in v1.0.5

type Promice[T any] func() (T, bool)

Promice returns two values: the evaluated value and if it is not skipped. It should be checked as: if p, notSkipped := promice(); notSkipped { appendToAns(p) }

Jump to

Keyboard shortcuts

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