Documentation ¶
Index ¶
- func After[T comparable](in []T, elem T) []T
- func AfterFn[T any](in []T, fn func(T) bool) []T
- func All[T any](slice []T, fn func(T) bool) bool
- func Any[T any](slice []T, fn func(T) bool) bool
- func AsRecv[T any](ch ...chan T) []<-chan T
- func AsSend[T any](ch ...chan T) []chan<- T
- func BFS[T any](root T, next func(T) []T, fn func(T))
- func BFSCtx[T any](ctx context.Context, root T, next func(T) []T, fn func(T))
- func Before[T comparable](in []T, elem T) []T
- func BeforeFn[T any](in []T, fn func(T) bool) []T
- func BindCtx(ctx context.Context, toCtx ...context.Context) context.Context
- func Binomial(trials int, p float64) int
- func Chunk[T any](slice []T, size int) [][]T
- func ChunkBy[T any](slice []T, cond ...ChunkCond[T]) [][]T
- func ChunkChan[T any](ch <-chan T, n int) <-chan []T
- func ChunkChanCtx[T any](ctx context.Context, ch <-chan T, n int) <-chan []T
- func Clone[T any](v []T) []T
- func CloseChan[T any](ch ...chan<- T)
- func CollectChan[T any](ch <-chan T) []T
- func CollectChanCtx[T any](ctx context.Context, ch <-chan T) []T
- func Concat[T any](in ...[]T) []T
- func ConcatChan[T any](chs ...<-chan T) <-chan T
- func ConcatChanCtx[T any](ctx context.Context, chs ...<-chan T) <-chan T
- func Contains[T comparable](in []T, elem T) bool
- func ContainsAll[T comparable](in []T, elems ...T) bool
- func ContainsAny[T comparable](in []T, elems ...T) bool
- func ContainsNone[T comparable](in []T, elems ...T) bool
- func Count[T comparable](in []T, val T) int
- func CountFn[T any](in []T, fn func(T) bool) int
- func DFS[T any](root T, next func(T) []T, fn func(T))
- func DFSCtx[T any](ctx context.Context, root T, next func(T) []T, fn func(T))
- func Difference[T comparable](a, b []T) []T
- func DifferenceFn[T any](a, b []T, eq func(T, T) bool) []T
- func DropChan[T any](ch <-chan T, n int) <-chan T
- func DropChanCtx[T any](ctx context.Context, ch <-chan T, n int) <-chan T
- func ElementsMatch[T comparable](in ...[]T) bool
- func ElementsMatchFn[T any](in [][]T, eq func(T, T) bool) bool
- func Equal[T comparable](in ...[]T) bool
- func EqualFn[T any](in [][]T, eq func(T, T) bool) bool
- func Factorial(n int) int
- func FanIn[T any](chs ...<-chan T) <-chan T
- func FanInCtx[T any](ctx context.Context, chs ...<-chan T) <-chan T
- func FanOut[T any](ch <-chan T, n int) []<-chan T
- func FanOutCtx[T any](ctx context.Context, ch <-chan T, n int) []<-chan T
- func Filter[T any](slice []T, fn func(T) bool) []T
- func FilterIdx[T any](slice []T, fn func(int, T) bool) []T
- func Flatten[T any](in [][]T) []T
- func FlattenChan[T any](ch <-chan []T) <-chan T
- func FlattenChanCtx[T any](ctx context.Context, ch <-chan []T) <-chan T
- func ForChan[T any](ch <-chan T, fn func(T))
- func ForChanCtx[T any](ctx context.Context, ch <-chan T, fn func(context.Context, T))
- func ForEach[T any](slice []T, fn func(T))
- func ForEachErr[T any](slice []T, fn func(T) error) error
- func ForEachIdx[T any](slice []T, fn func(int, T))
- func ForEachIdxErr[T any](slice []T, fn func(int, T) error) error
- func ForN(n int, fn func(int))
- func ForNErr(n int, fn func(int) error) error
- func ForWindow[T any](slice []T, size int, fn func([]T))
- func ForWindowErr[T any](slice []T, size int, fn func([]T) error) error
- func ForWindowIdx[T any](slice []T, size int, fn func(int, []T))
- func ForWindowIdxErr[T any](slice []T, size int, fn func(int, []T) error) error
- func Forward[T any](src []<-chan T, dst []chan<- T)
- func ForwardCtx[T any](ctx context.Context, src []<-chan T, dst []chan<- T)
- func GoForEach[T any](ctx context.Context, slice []T, fn func(context.Context, T))
- func GoForEachErr[T any](ctx context.Context, slice []T, fn func(context.Context, T) error) error
- func GoForEachIdx[T any](ctx context.Context, slice []T, fn func(context.Context, int, T))
- func GoForEachIdxErr[T any](ctx context.Context, slice []T, fn func(context.Context, int, T) error) error
- func GoForN(ctx context.Context, n int, fn func(context.Context, int))
- func GoForNErr(ctx context.Context, n int, fn func(context.Context, int) error) error
- func GoForWindow[T any](ctx context.Context, slice []T, size int, fn func(context.Context, []T))
- func GoForWindowErr[T any](ctx context.Context, slice []T, size int, fn func(context.Context, []T) error) error
- func GoForWindowIdx[T any](ctx context.Context, slice []T, size int, fn func(context.Context, int, []T))
- func GoForWindowIdxErr[T any](ctx context.Context, slice []T, size int, ...) error
- func GoMapChan[T, R any](ch <-chan T, fn func(T) R) <-chan R
- func GoMapChanCtx[T, R any](ctx context.Context, ch <-chan T, fn func(context.Context, T) R) <-chan R
- func GoMapEach[T, U any](ctx context.Context, slice []T, fn func(context.Context, T) U) []U
- func GoMapEachErr[T, U any](ctx context.Context, slice []T, fn func(context.Context, T) (U, error)) ([]U, error)
- func GoMapEachIdx[T, U any](ctx context.Context, slice []T, fn func(context.Context, int, T) U) []U
- func GoMapEachIdxErr[T, U any](ctx context.Context, slice []T, fn func(context.Context, int, T) (U, error)) ([]U, error)
- func GoMapN[T any](ctx context.Context, n int, fn func(context.Context, int) T) []T
- func GoMapNErr[T any](ctx context.Context, n int, fn func(context.Context, int) (T, error)) ([]T, error)
- func GoMapWindow[T, U any](ctx context.Context, slice []T, size int, fn func(context.Context, []T) U) []U
- func GoMapWindowErr[T, U any](ctx context.Context, slice []T, size int, ...) ([]U, error)
- func GoMapWindowIdx[T, U any](ctx context.Context, slice []T, size int, fn func(context.Context, int, []T) U) []U
- func GoMapWindowIdxErr[T, U any](ctx context.Context, slice []T, size int, ...) ([]U, error)
- func Index[T comparable](in []T, elem T) int
- func IndexAll[T comparable](in []T, elem T) []int
- func IndexAllFn[T any](in []T, fn func(T) bool) []int
- func IndexFn[T any](in []T, fn func(T) bool) int
- func Insert[T any](in []T, idx int, elems ...T) []T
- func Intersect[T comparable](in ...[]T) []T
- func IntersectFn[T any](in [][]T, eq func(T, T) bool) []T
- func Last[T any](in []T) T
- func Lazy[T any](fn func() T) func() T
- func MapChan[T, R any](ch <-chan T, fn func(T) R) <-chan R
- func MapChanCtx[T, R any](ctx context.Context, ch <-chan T, fn func(context.Context, T) R) <-chan R
- func MapEach[T, U any](slice []T, fn func(T) U) []U
- func MapEachErr[T, U any](slice []T, fn func(T) (U, error)) ([]U, error)
- func MapEachIdx[T, U any](slice []T, fn func(int, T) U) []U
- func MapEachIdxErr[T, U any](slice []T, fn func(int, T) (U, error)) ([]U, error)
- func MapN[T any](n int, fn func(int) T) []T
- func MapNErr[T any](n int, fn func(int) (T, error)) ([]T, error)
- func MapWindow[T, U any](slice []T, size int, fn func([]T) U) []U
- func MapWindowErr[T, U any](slice []T, size int, fn func([]T) (U, error)) ([]U, error)
- func MapWindowIdx[T, U any](slice []T, size int, fn func(int, []T) U) []U
- func MapWindowIdxErr[T, U any](slice []T, size int, fn func(int, []T) (U, error)) ([]U, error)
- func Max[T constraints.Ordered](in ...T) T
- func MaxFn[T any](in []T, fn func(T, T) bool) T
- func MaxIdx[T constraints.Ordered](in ...T) int
- func MaxIdxFn[T any](in []T, fn func(T, T) bool) int
- func Min[T constraints.Ordered](in ...T) T
- func MinFn[T any](in []T, fn func(T, T) bool) T
- func MinIdx[T constraints.Ordered](in ...T) int
- func MinIdxFn[T any](in []T, fn func(T, T) bool) int
- func Must[T any](v T, err error) T
- func NewBufPipe[T any](size int) (chan<- T, <-chan T)
- func NewPipe[T any]() (chan<- T, <-chan T)
- func NewPipeline[In, Out any](ctx context.Context, buf int, fn func(context.Context, In) Result[Out]) (chan<- In, <-chan Result[Out])
- func None[T any](slice []T, fn func(T) bool) bool
- func Normal(mean, stddev float64) float64
- func OnCancel(ctx context.Context, fn func())
- func Perms[T any](in []T) [][]T
- func PermsIdx(n int) [][]int
- func PickUniform[T any](slice ...T) T
- func PickWeighted[T any](slice []T, weights []int) T
- func Pointer[T any](v T) *T
- func Power[T any](in []T) [][]T
- func PowerIdx(n int) [][]int
- func Range(start, end int) []int
- func RangeN(n int) []int
- func RecvFrom[T any](chs ...<-chan T) (T, bool)
- func RecvFromCtx[T any](ctx context.Context, chs ...<-chan T) (T, bool)
- func Reduce[T, U any](slice []T, init U, fn func(U, T) U) U
- func ReduceIdx[T, U any](slice []T, init U, fn func(U, int) U) U
- func Remove[T comparable](in []T, elems ...T) []T
- func RemoveFn[T any](in []T, fn func(T) bool) []T
- func RemoveIdx[T any](in []T, indices ...int) []T
- func RemoveN[T any](in []T, idx, n int) []T
- func RemoveRange[T any](in []T, start, end int) []T
- func RepeatN[T any](n int, value T) []T
- func Reverse[T any](in []T) []T
- func Same[T comparable](in ...T) bool
- func SameFn[T any](in []T, fn func(T, T) bool) bool
- func SendTo[T any](v T, chs ...chan<- T)
- func SendToCtx[T any](ctx context.Context, v T, chs ...chan<- T)
- func Set[T comparable](in []T) map[T]struct{}
- func Shuffle[T any](in []T) []T
- func Sort[T constraints.Ordered](in []T) []T
- func SortFn[T any](in []T, fn func(T, T) bool) []T
- func Sum[T constraints.Ordered](in []T) T
- func TakeChan[T any](ch <-chan T, n int) <-chan T
- func TakeChanCtx[T any](ctx context.Context, ch <-chan T, n int) <-chan T
- func Uniform(min, max float64) float64
- func Uniq[T comparable](in []T) []T
- func UniqFn[T any](in []T, eq func(T, T) bool) []T
- func Unzip[T any](in [][]T) [][]T
- func Weighted(weights []int) int
- func WithParallelism(ctx context.Context, n int) context.Context
- func Zero[T any]() T
- func Zip[T any](in ...[]T) [][]T
- func ZipChan[T any](chs ...<-chan T) <-chan []T
- func ZipChanCtx[T any](ctx context.Context, chs ...<-chan T) <-chan []T
- type ChunkCond
- type Future
- type Gate
- type Group
- type Iter
- func ChanIter[T any](ch <-chan T) Iter[T]
- func ChanIterCtx[T any](ctx context.Context, ch <-chan T) Iter[T]
- func ChunkIter[T any](iter Iter[T], size int) Iter[[]T]
- func FlattenIter[T any](iter Iter[Iter[T]]) Iter[T]
- func FuncIter[T any](fn func() (T, bool)) Iter[T]
- func JoinIter[T any](iters ...Iter[T]) Iter[T]
- func MapIter[T, U any](iter Iter[T], fn func(T) U) Iter[U]
- func NewIter[T any](r Readable[T]) Iter[T]
- func SliceIter[T any](slice []T) Iter[T]
- func ZipIter[T any](iters ...Iter[T]) Iter[[]T]
- type Job
- type Map
- type MinMaxCounter
- type Peeker
- type Pool
- type Queue
- type Readable
- type Result
- type Semaphore
- type Singular
- type Waiter
- type Writable
- type Writer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶ added in v0.6.0
func After[T comparable](in []T, elem T) []T
After returns all elements in the given slice after the given element.
func AfterFn ¶ added in v0.6.0
AfterFn returns all elements in the given slice after the first element that satisfies the given function.
func AsRecv ¶
func AsRecv[T any](ch ...chan T) []<-chan T
AsRecv converts the given channels to receive-only channels.
func AsSend ¶
func AsSend[T any](ch ...chan T) []chan<- T
AsSend converts the given channels to send-only channels.
func BFS ¶ added in v0.3.0
func BFS[T any](root T, next func(T) []T, fn func(T))
BFS is a breadth-first search starting at the root node and traversing the graph using the next function.
func BFSCtx ¶ added in v0.3.0
BFSCtx is a breadth-first search starting at the root node and traversing the graph using the next function. The context can be used to cancel the search.
func Before ¶ added in v0.6.0
func Before[T comparable](in []T, elem T) []T
Before returns all elements in the given slice before the given element.
func BeforeFn ¶ added in v0.6.0
BeforeFn returns all elements in the given slice before the first element that satisfies the given function.
func BindCtx ¶ added in v0.9.0
BindCtxAny binds the first given context to the rest of the given contexts. The returned context is cancelled once any of the given contexts are cancelled.
func Binomial ¶ added in v0.3.0
Binomial returns a random number from a binomial distribution with the given number of trials and probability of success.
func ChunkBy ¶ added in v0.13.0
ChunkBy chunks the given slice into slices limited by the given conditions.
func ChunkChan ¶ added in v0.12.0
ChunkChan chunks the given channel into a single channel. The returned channel is closed when the given channel is closed.
func ChunkChanCtx ¶ added in v0.12.0
ChunkChanCtx chunks the given channel into a single channel. The returned channel is closed when the given channel is closed or the context is canceled.
func CollectChan ¶
func CollectChan[T any](ch <-chan T) []T
CollectChan reads all values from the channel.
func CollectChanCtx ¶
CollectChanCtx reads all values from the channel. It stops reading when the context is canceled.
func Concat ¶
func Concat[T any](in ...[]T) []T
Concat returns a slice containing the elements of the given slices.
func ConcatChan ¶
func ConcatChan[T any](chs ...<-chan T) <-chan T
ConcatChan joins the given channels into a single channel. That is, the first channel is emptied, then the second, and so on. The returned channel is closed when all the given channels are closed.
func ConcatChanCtx ¶
ConcatChanCtx joins the given channels into a single channel. That is, the first channel is emptied, then the second, and so on. The returned channel is closed when all the given channels are closed or the context is canceled.
func Contains ¶
func Contains[T comparable](in []T, elem T) bool
Contains returns true if the given slice contains the given element.
func ContainsAll ¶
func ContainsAll[T comparable](in []T, elems ...T) bool
ContainsAll returns true if the given slice contains all of the given elements.
func ContainsAny ¶
func ContainsAny[T comparable](in []T, elems ...T) bool
ContainsAny returns true if the given slice contains any of the given elements.
func ContainsNone ¶
func ContainsNone[T comparable](in []T, elems ...T) bool
ContainsNone returns true if the given slice contains none of the given elements.
func Count ¶
func Count[T comparable](in []T, val T) int
Count returns the number of elements in the given slice that equal the given value.
func CountFn ¶
CountFn returns the number of elements in the given slice that satisfy the given predicate.
func DFS ¶ added in v0.3.0
func DFS[T any](root T, next func(T) []T, fn func(T))
DFS is a depth-first search starting at the root node and traversing the graph using the next function.
func DFSCtx ¶ added in v0.3.0
DFSCtx is a depth-first search starting at the root node and traversing the graph using the next function. The context can be used to cancel the search.
func Difference ¶
func Difference[T comparable](a, b []T) []T
Difference returns a slice of the elements that are in the first slice but not the second.
func DifferenceFn ¶
DifferenceFn returns a slice of the elements that are in the first slice but not the second, according to the given function.
func DropChan ¶ added in v0.7.0
DropChan returns a channel on which n values are dropped from the given channel.
func DropChanCtx ¶ added in v0.7.0
DropChanCtx returns a channel on which n values are dropped from the given channel. It stops reading when the context is canceled.
func ElementsMatch ¶ added in v0.7.0
func ElementsMatch[T comparable](in ...[]T) bool
ElementsMatch returns true if the given slices contain the same elements.
func ElementsMatchFn ¶ added in v0.7.0
ElementsMatchFn returns true if the given slices contain the same elements, according to the given function.
func Equal ¶
func Equal[T comparable](in ...[]T) bool
Equal returns true if the given slices are equal.
func FanIn ¶
func FanIn[T any](chs ...<-chan T) <-chan T
FanIn merges the given channels into a single channel. That is, values are read from each channel in parallel. The returned channel is closed when all the given channels are closed.
func FanInCtx ¶
FanInCtx merges the given channels into a single channel. That is, values are read from each channel in parallel. The returned channel is closed when all the given channels are closed or the context is canceled.
func FanOut ¶
FanOut splits the given channel into n channels. The returned channels are closed when the given channel is closed.
func FanOutCtx ¶
FanOutCtx splits the given channel into n channels. The returned channels are closed when the given channel is closed or the context is canceled.
func Filter ¶
Filter returns a slice of the elements in the given slice that satisfy the given predicate.
func FilterIdx ¶
FilterIdx returns a slice of the elements in the given slice that satisfy the given predicate.
func Flatten ¶ added in v0.12.0
func Flatten[T any](in [][]T) []T
Flatten returns a slice of the elements in the given slices.
func FlattenChan ¶ added in v0.12.0
func FlattenChan[T any](ch <-chan []T) <-chan T
FlattenChan flattens the given channel into a single channel. The returned channel is closed when the given channel is closed.
func FlattenChanCtx ¶ added in v0.12.0
FlattenChanCtx flattens the given channel into a single channel. The returned channel is closed when the given channel is closed or the context is canceled.
func ForChan ¶
func ForChan[T any](ch <-chan T, fn func(T))
ForChan calls the given function for each value in the channel.
func ForChanCtx ¶
ForChanCtx calls the given function for each value in the channel. It stops iterating when the context is canceled.
func ForEach ¶ added in v0.3.0
func ForEach[T any](slice []T, fn func(T))
ForEach calls the given function for each element in the given slice.
func ForEachErr ¶ added in v0.3.0
ForEachErr calls the given function for each element in the given slice.
func ForEachIdx ¶ added in v0.3.0
ForEachIdx calls the given function for each index of the given slice.
func ForEachIdxErr ¶ added in v0.3.0
ForEachIdxErr calls the given function for each index of the given slice.
func ForWindow ¶
ForWindow calls the given function for each window of the given size in the given slice.
func ForWindowErr ¶ added in v0.3.0
ForWindowErr calls the given function for each window of the given size in the given slice.
func ForWindowIdx ¶
ForWindowIdx calls the given function for each window of the given size in the given slice.
func ForWindowIdxErr ¶ added in v0.3.0
ForWindowIdxErr calls the given function for each window of the given size in the given slice.
func Forward ¶ added in v0.7.0
func Forward[T any](src []<-chan T, dst []chan<- T)
Forward forwards values from the src channel(s) to the dst channel(s).
func ForwardCtx ¶ added in v0.7.0
ForwardCtx forwards values from the src channel(s) to the dst channel(s). It stops forwarding when the context is canceled.
func GoForEach ¶ added in v0.3.0
GoForEach calls the given function with each value in the given slice in parallel.
func GoForEachErr ¶ added in v0.3.0
GoForEachErr calls the given function with each value in the given slice in parallel.
func GoForEachIdx ¶ added in v0.3.0
GoForEachIdx calls the given function with each value in the given slice in parallel.
func GoForEachIdxErr ¶ added in v0.3.0
func GoForEachIdxErr[T any](ctx context.Context, slice []T, fn func(context.Context, int, T) error) error
GoForEachIdxErr calls the given function with each value in the given slice in parallel.
func GoForNErr ¶ added in v0.3.0
GoForNErr calls the given function with n unique values in parallel.
func GoForWindow ¶
GoForWindow calls the given function for each window of the given size in the given slice in parallel.
func GoForWindowErr ¶ added in v0.3.0
func GoForWindowErr[T any](ctx context.Context, slice []T, size int, fn func(context.Context, []T) error) error
GoForWindowErr calls the given function for each window of the given size in the given slice in parallel.
func GoForWindowIdx ¶
func GoForWindowIdx[T any](ctx context.Context, slice []T, size int, fn func(context.Context, int, []T))
GoForWindowIdx calls the given function for each window of the given size in the given slice in parallel.
func GoForWindowIdxErr ¶ added in v0.3.0
func GoForWindowIdxErr[T any](ctx context.Context, slice []T, size int, fn func(context.Context, int, []T) error) error
GoForWindowIdxErr calls the given function for each window of the given size in the given slice in parallel.
func GoMapChan ¶ added in v0.5.0
func GoMapChan[T, R any](ch <-chan T, fn func(T) R) <-chan R
GoMapChan returns a channel that applies the given function to each value in the input channel in parallel.
func GoMapChanCtx ¶ added in v0.5.0
func GoMapChanCtx[T, R any](ctx context.Context, ch <-chan T, fn func(context.Context, T) R) <-chan R
GoMapChanCtx returns a channel that applies the given function to each value in the input channel in parallel. It stops iterating when the context is canceled.
func GoMapEach ¶ added in v0.3.0
GoMapEach returns a slice of the results of the given function applied to each element in the given slice in parallel.
func GoMapEachErr ¶ added in v0.3.0
func GoMapEachErr[T, U any](ctx context.Context, slice []T, fn func(context.Context, T) (U, error)) ([]U, error)
GoMapEachErr returns a slice of the results of the given function applied to each element in the given slice in parallel.
func GoMapEachIdx ¶ added in v0.3.0
GoMapEachIdx returns a slice of the results of the given function applied to each index of the given slice in parallel.
func GoMapEachIdxErr ¶ added in v0.3.0
func GoMapEachIdxErr[T, U any](ctx context.Context, slice []T, fn func(context.Context, int, T) (U, error)) ([]U, error)
GoMapEachIdxErr returns a slice of the results of the given function applied to each index of the given slice in parallel.
func GoMapN ¶
GoMapN returns a slice of the results of the given function applied in parallel to each index in the given range.
func GoMapNErr ¶ added in v0.3.0
func GoMapNErr[T any](ctx context.Context, n int, fn func(context.Context, int) (T, error)) ([]T, error)
GoMapNErr returns a slice of the results of the given function applied in parallel to each index in the given range.
func GoMapWindow ¶
func GoMapWindow[T, U any](ctx context.Context, slice []T, size int, fn func(context.Context, []T) U) []U
GoMapWindow returns a slice of the results of the given function applied to each window of the given size in the given slice in parallel.
func GoMapWindowErr ¶ added in v0.3.0
func GoMapWindowErr[T, U any](ctx context.Context, slice []T, size int, fn func(context.Context, []T) (U, error)) ([]U, error)
GoMapWindowErr returns a slice of the results of the given function applied to each window of the given size in the given slice in parallel.
func GoMapWindowIdx ¶
func GoMapWindowIdx[T, U any](ctx context.Context, slice []T, size int, fn func(context.Context, int, []T) U) []U
GoMapWindowIdx returns a slice of the results of the given function applied to each window of the given size in the given slice in parallel.
func GoMapWindowIdxErr ¶ added in v0.3.0
func GoMapWindowIdxErr[T, U any](ctx context.Context, slice []T, size int, fn func(context.Context, int, []T) (U, error)) ([]U, error)
GoMapWindowIdxErr returns a slice of the results of the given function applied to each window of the given size in the given slice in parallel.
func Index ¶
func Index[T comparable](in []T, elem T) int
Index returns the index of the given element in the given slice, or -1 if it is not found.
func IndexAll ¶
func IndexAll[T comparable](in []T, elem T) []int
IndexAll returns the indices of all elements that are equal to the given element in the given slice.
func IndexAllFn ¶
IndexAllFn returns the indices of all elements that satisfy the given function in the given slice.
func IndexFn ¶
IndexFn returns the index of the first element that satisfies the given function in the given slice, or -1 if it is not found.
func Intersect ¶
func Intersect[T comparable](in ...[]T) []T
Intersect returns a slice of the elements that are in both the given slices.
func IntersectFn ¶
IntersectFn returns a slice of the elements that are in both the given slices, according to the given function.
func Lazy ¶ added in v0.3.0
func Lazy[T any](fn func() T) func() T
Lazy is a lazy value. It is initialized once via fn and then cached. It is safe for concurrent use.
func MapChan ¶ added in v0.5.0
func MapChan[T, R any](ch <-chan T, fn func(T) R) <-chan R
MapChan returns a channel that applies the given function to each value in the input channel.
func MapChanCtx ¶ added in v0.5.0
MapChanCtx returns a channel that applies the given function to each value in the input channel. It stops iterating when the context is canceled.
func MapEach ¶ added in v0.3.0
func MapEach[T, U any](slice []T, fn func(T) U) []U
MapEach returns a slice of the results of the given function applied to each element in the given slice.
func MapEachErr ¶ added in v0.3.0
MapEachErr returns a slice of the results of the given function applied to each element in the given slice.
func MapEachIdx ¶ added in v0.3.0
MapEachIdx returns a slice of the results of the given function applied to each index of the given slice.
func MapEachIdxErr ¶ added in v0.3.0
MapEachIdxErr returns a slice of the results of the given function applied to each index of the given slice.
func MapN ¶
MapN returns a slice of the results of the given function applied to each index in the given range.
func MapNErr ¶ added in v0.3.0
MapNErr returns a slice of the results of the given function applied to each index in the given range.
func MapWindow ¶
MapWindow returns a slice of the results of the given function applied to each window of the given size in the given slice.
func MapWindowErr ¶ added in v0.3.0
MapWindowErr returns a slice of the results of the given function applied to each window of the given size in the given slice.
func MapWindowIdx ¶
MapWindowIdx returns a slice of the results of the given function applied to each window of the given size in the given slice.
func MapWindowIdxErr ¶ added in v0.3.0
MapWindowIdxErr returns a slice of the results of the given function applied to each window of the given size in the given slice.
func Max ¶
func Max[T constraints.Ordered](in ...T) T
Max returns the maximum value in the given slice.
func MaxIdx ¶
func MaxIdx[T constraints.Ordered](in ...T) int
MaxIdx returns the index of the maximum element in the given slice.
func MaxIdxFn ¶
MaxIdxFn returns the index of the maximum element in the given slice, according to the given function.
func Min ¶
func Min[T constraints.Ordered](in ...T) T
Min returns the minimum value in the given slice.
func MinIdx ¶
func MinIdx[T constraints.Ordered](in ...T) int
MinIdx returns the index of the minimum element in the given slice.
func MinIdxFn ¶
MinIdxFn returns the index of the minimum element in the given slice, according to the given function.
func NewBufPipe ¶
NewBufPipe returns a pair of connected channels, one for sending and one for receiving. Writes to the in channel block when the buffer is full. The out channel is closed when the in channel is closed.
Example ¶
package main import ( "fmt" "github.com/go-dj/dj" ) func main() { // Create a new pipe for ints with a buffer of size 3. in, out := dj.NewBufPipe[int](3) // Write some values to the pipe. in <- 1 in <- 2 in <- 3 // The buffer is full, so the write blocks. select { case in <- 4: panic("write should block") default: // ... } // Read the values from the pipe. fmt.Println(<-out) // 1 fmt.Println(<-out) // 2 fmt.Println(<-out) // 3 // The buffer is empty, so the read blocks. select { case <-out: panic("read should block") default: // ... } }
Output: 1 2 3
func NewPipe ¶
func NewPipe[T any]() (chan<- T, <-chan T)
NewPipe returns a pair of connected channels, one for sending and one for receiving. Sent values are buffered until they are received. The out channel is closed when the in channel is closed.
Example ¶
package main import ( "fmt" "github.com/go-dj/dj" ) func main() { // Create a new pipe for ints. in, out := dj.NewPipe[int]() // Write some values to the pipe. // Writes never block; they are buffered until they are read. go func() { in <- 1 in <- 2 in <- 3 // Close the pipe to signal that no more values will be written. // Buffered values can still be read. close(in) }() // Read the values from the pipe. for v := range out { fmt.Println(v) } }
Output: 1 2 3
func NewPipeline ¶ added in v0.5.0
func NewPipeline[In, Out any](ctx context.Context, buf int, fn func(context.Context, In) Result[Out]) (chan<- In, <-chan Result[Out])
NewPipeline returns a pair of connected channels. Values sent to the first channel are processed by fn and the results are sent to the second channel. If buf is greater than or equal to zero, up to buf values are buffered before the pipeline blocks. Otherwise, writes never block.
func Normal ¶ added in v0.3.0
Normal returns a random number from a normal distribution with the given mean and standard deviation.
func OnCancel ¶ added in v0.12.0
OnCancel registers a function to be called when the given context is cancelled.
func Perms ¶
func Perms[T any](in []T) [][]T
Perms returns a slice of all the possible permutations of the given slice.
func PermsIdx ¶
PermsIdx returns a slice containing the indices of all the possible permutations of a slice of the given length.
func PickUniform ¶ added in v0.9.0
func PickUniform[T any](slice ...T) T
PickUniform returns an element from the given slice, chosen uniformly at random.
func PickWeighted ¶ added in v0.10.0
PickWeighted returns an element from the given slice, chosen at random according to the given weights.
func Power ¶
func Power[T any](in []T) [][]T
Power returns a slice of all the possible combinations of the given slice.
func PowerIdx ¶
PowerIdx returns a slice containing the indices of all the possible combinations of a slice of the given length.
func RecvFrom ¶ added in v0.5.0
RecvFrom receives a value from one of the given channels. It blocks until a value is received, which it returns. The boolean indicates whether the read was successful; it is false if the channel is closed.
func RecvFromCtx ¶ added in v0.5.0
RecvFromCtx receives a value from one of the given channels. It blocks until a value is received, which it returns, or the context is canceled. The boolean indicates whether the read was successful; it is false if the channel is closed.
func Reduce ¶
func Reduce[T, U any](slice []T, init U, fn func(U, T) U) U
Reduce returns the result of applying the given function to each element in the given slice, starting with the given initial value.
func ReduceIdx ¶
ReduceIdx returns the result of applying the given function to each index of the given slice, starting with the given initial value.
func Remove ¶
func Remove[T comparable](in []T, elems ...T) []T
Remove returns a slice with the given elements removed.
func RemoveRange ¶
RemoveRange returns a slice with the elements in the given range removed.
func Same ¶
func Same[T comparable](in ...T) bool
Same returns true if all the given values are equal.
func SameFn ¶
SameFn returns true if all the given values are equal, according to the given function.
func SendTo ¶ added in v0.5.0
func SendTo[T any](v T, chs ...chan<- T)
SendTo sends the given value to one of the given channels. It blocks until the value is sent.
func SendToCtx ¶ added in v0.5.0
SendToCtx sends the given value to one of the given channels. It blocks until the value is sent or the context is canceled.
func Set ¶
func Set[T comparable](in []T) map[T]struct{}
SetFn returns a map set of the elements in the given slice, with duplicates removed.
func Sort ¶
func Sort[T constraints.Ordered](in []T) []T
Sort returns a sorted slice of the given slice.
func TakeChanCtx ¶
TakeChanCtx returns a channel on which n values are taken from the given channel. It stops reading when the context is canceled.
func Uniform ¶ added in v0.3.0
Uniform returns a random number from a uniform distribution between the given min and max values.
func Uniq ¶
func Uniq[T comparable](in []T) []T
Uniq returns a slice of the unique elements in the given slice.
func UniqFn ¶
UniqFn returns a slice of the unique elements in the given slice, according to the given function.
func Unzip ¶
func Unzip[T any](in [][]T) [][]T
Unzip returns a slice of slices, where each slice contains the elements at the same index in the given tuples.
func WithParallelism ¶ added in v0.3.0
WithParallelism returns a context with the given parallelism. Certain functions in this package will use this value to determine how many goroutines to run concurrently. If the context already has a parallelism value, it will be overwritten. If not set, the default parallelism is the number of CPUs.
func Zip ¶
func Zip[T any](in ...[]T) [][]T
Zip returns a slice of tuples, where each tuple contains the elements at the same index in the given slices.
func ZipChan ¶
func ZipChan[T any](chs ...<-chan T) <-chan []T
ZipChan zips the given channels into a single channel. The returned channel is closed when all the given channels are closed.
func ZipChanCtx ¶
ZipChanCtx zips the given channels into a single channel. The returned channel is closed when all the given channels are closed or the context is canceled.
Types ¶
type Future ¶ added in v0.3.0
type Future[T any] struct { // contains filtered or unexported fields }
Future represents a value that will be available in the future. Its value is not available until the future is resolved, at which point it can be retrieved using Get.
func Then ¶ added in v0.7.0
Then returns a new Future that is resolved when the current Future is resolved. The resolver function is called with the value of the current Future. If the current Future is resolved with an error, the resolver function is not called and the returned Future is resolved with the same error.
type Gate ¶ added in v0.9.0
type Gate struct {
// contains filtered or unexported fields
}
Gate is a type that can block and unblock execution. The gate can be opened with Open() and closed with Close(). Calling Wait() will block until the gate is opened. If the gate is already open, Wait() will return immediately.
func (*Gate) Close ¶ added in v0.9.0
func (g *Gate) Close()
Close closes the gate. If the gate is already closed, this is a no-op.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a group of goroutines.
func NewGroup ¶
NewGroup returns a new group of goroutines. The given context is used to cancel all goroutines in the group. The number of goroutines permitted to run concurrently is given by n.
func (*Group) Cancel ¶ added in v0.3.0
func (s *Group) Cancel()
Cancel cancels all goroutines in the group. This is destructive and cannot be undone; the group cannot be reused.
func (*Group) GoN ¶
GoN runs the given function n times concurrently in a child group and returns the child group.
type Iter ¶
type Iter[T any] interface { Readable[T] // Collect returns a slice containing all the values in the iterator. Collect() []T // Take returns a new iterator that will return at most n values from the original iterator. Take(n int) Iter[T] // Drop returns a new iterator that will drop the first n values from the original iterator. Drop(n int) Iter[T] // Filter returns a new iterator that will only return values for which the given predicate returns true. Filter(fn func(T) bool) Iter[T] // Chan returns a channel that will receive all the values in the iterator. Chan() <-chan T }
Iter is a type that can be read sequentially.
func ChanIterCtx ¶
ChanIterCtx returns an iterator over the given channel, which will be closed when the given context is canceled.
func ChunkIter ¶
ChunkIter returns an iterator over the given iterator, chunking the values into slices of the given size.
func FlattenIter ¶
FlattenIter returns an iterator over the given iterator, flattening nested iterators. That is, it converts an iterator over iterators into an iterator over the values of those iterators.
type Job ¶
type Job[Req, Res any] struct { // contains filtered or unexported fields }
Job is a Job that has been submitted to a worker pool.
type Map ¶
type Map[Key comparable, Value any] struct { // contains filtered or unexported fields }
func NewMap ¶ added in v0.3.0
func NewMap[Key comparable, Value any]() *Map[Key, Value]
type MinMaxCounter ¶ added in v0.9.0
type MinMaxCounter struct {
// contains filtered or unexported fields
}
MinMaxCounter is a thread-safe counter which records the minimum and maximum values it has reached.
func (*MinMaxCounter) Add ¶ added in v0.9.0
func (c *MinMaxCounter) Add(v int)
Add adds the given value to the counter.
func (*MinMaxCounter) Current ¶ added in v0.9.0
func (c *MinMaxCounter) Current() int
Current returns the current value of the counter.
func (*MinMaxCounter) Max ¶ added in v0.9.0
func (c *MinMaxCounter) Max() int
Max returns the maximum value the counter has reached.
func (*MinMaxCounter) Min ¶ added in v0.9.0
func (c *MinMaxCounter) Min() int
Min returns the minimum value the counter has reached.
type Peeker ¶
type Peeker[T any] interface { Iter[T] // Peek returns the next value in the iterator without advancing it. Peek() (T, bool) }
Peeker is a peekable iterator.
type Pool ¶ added in v0.7.0
type Pool[Req, Res any] struct { // contains filtered or unexported fields }
Pool is a pool of workers that can be used to process work concurrently. Jobs of type Req are submitted to the pool and are processed by a worker. The result of the job is returned as a value of type Res.
func NewPool ¶ added in v0.7.0
func NewPool[Req, Res any](ctx context.Context, weight int, fn func(context.Context, Req) Result[Res]) *Pool[Req, Res]
NewPool returns a new worker pool which handles jobs of type Req up to the given weight. The given function is called for each job submitted to the pool, returning a Result[Res].
Example ¶
package main import ( "context" "fmt" "strconv" "github.com/go-dj/dj" ) func main() { // Create a pool of weight 2. pool := dj.NewPool(context.Background(), 2, func(_ context.Context, in string) dj.Result[int] { return dj.NewResult(strconv.Atoi(in)) }) defer pool.Close() // Submit 3 jobs to the pool. job1 := pool.Submit(context.Background(), 1, "1") job2 := pool.Submit(context.Background(), 1, "foo") job3 := pool.Submit(context.Background(), 1, "3") // Each job should be processed. fmt.Println(job1.Await().Unpack()) fmt.Println(job2.Await().Unpack()) fmt.Println(job3.Await().Unpack()) // Submit a list of jobs to the pool. fmt.Println(pool.Process(context.Background(), 1, "4", "5", "6")) // If any job fails, the entire list of jobs will fail. fmt.Println(pool.Process(context.Background(), 1, "7", "foo", "9")) }
Output: 1 <nil> 0 strconv.Atoi: parsing "foo": invalid syntax 3 <nil> [4 5 6] <nil> [] strconv.Atoi: parsing "foo": invalid syntax
func (*Pool[Req, Res]) Close ¶ added in v0.8.0
func (p *Pool[Req, Res]) Close()
Close closes the pool. Subsequent calls to Submit will panic.
type Readable ¶ added in v0.3.0
type Readable[T any] interface { // Read returns the next value in the iterator. // If the iterator is exhausted, the boolean will be false. Read() (T, bool) }
Readable is a type that can be iterated over by repeatedly calling Read.
type Result ¶ added in v0.5.0
type Result[T any] struct { // contains filtered or unexported fields }
Result is a type that represents a value or an error.
func (Result[T]) Err ¶ added in v0.7.0
Err returns the error of the Result. If the Result is a value, the error is nil.
type Semaphore ¶ added in v0.12.0
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore is a weighted semaphore.
func NewSemaphore ¶ added in v0.12.0
NewSemaphore returns a new weighted semaphore.
func (*Semaphore) Acquire ¶ added in v0.12.0
Acquire acquires n resources. If the context is canceled, the acquisition is aborted and an error is returned.
type Singular ¶ added in v0.3.0
type Singular[T any] struct { // contains filtered or unexported fields }
Singular obtains a value exactly once. Any subsequent attempts to set the value will be ignored.
type Waiter ¶ added in v0.14.0
type Waiter struct {
// contains filtered or unexported fields
}
Waiter allows goroutines to wait for a signal. When Signal() is called, all goroutines waiting on Wait() will be unblocked. Subsequent calls to Wait() will block again.
type Writer ¶
Writer is a type that can be written to by repeatedly calling Write.
func ChanWriter ¶
ChanWriter returns a writer that writes to the given channel.
func ChanWriterCtx ¶
ChanWriterCtx returns a writer that writes to the given channel until the given context is canceled.
func FuncWriter ¶
FuncWriter returns a writer that calls the given function for each value to be written.