slicer

package
v1.0.37 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: MIT Imports: 5 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGroup = errors.New("parition error")
)
View Source
var (
	ErrPartition = errors.New("parition error")
)
View Source
var (
	ErrReduceExit = errors.New("reduce exit")
)

Functions

func All

func All[T comparable](match T, elems ...T) bool

All[T] test if the match value is equal to all elements in elems

func AllFn

func AllFn[T any](fn func(int, T) bool, elems ...T) bool

All[T] test if the result of fn(int, T) evaluates to true for all elements in elems

The fn(int, T) bool functions takes:

  • int is the index of the current element
  • T is the current element

func AllFnErr

func AllFnErr[T any](fn func(int, T) (bool, error), elems ...T) (bool, error)

All[T] test if the result of fn(int, T) evaluates to true for all elements in elems

The fn(int, T) bool functions takes:

  • int is the index of the current element
  • T is the current element

If the fn function returns an error, processing through elems is stopped and the error is returned.

func Chunk

func Chunk[T any](size int, elems ...T) [][]T

Chunk creates a slice where each element is a slice of elems of size length.

If elems can't be split evenly, the final element will contain the remaining elems. If count < 1 the results are return as if count is 1

Example:

elems := []int{1, 2, 3, 4, 5}
size := 2
chunks := Chunk(size, elems...)
fmt.Println(chunks)
// [[1 2] [3 4] [5]]

func Contains

func Contains[T comparable](match T, elems ...T) bool

Contains[T] test if the match value is equal to any of the elements in elems

func ContainsFn

func ContainsFn[T any](fn func(int, T) bool, elems ...T) bool

Contains[T] test if the value returned from fn is true for any of the elements in elems

The fn(int, T) bool functions takes:

  • int is the index of the current element
  • T is the current element

func ContainsFnErr

func ContainsFnErr[T any](fn func(int, T) (bool, error), elems ...T) (bool, error)

Contains[T] test if the value returned from fn is true for any of the elements in elems

The fn(int, T) bool functions takes:

  • int is the index of the current element
  • T is the current element

If the fn function returns an error, processing through elems is stopped and the error is returned.

func Deduplicate added in v1.0.33

func Deduplicate[T comparable](elems ...T) []T

Deduplicate[T] creates a copy of elems with duplicates removed

func DeduplicateFn added in v1.0.33

func DeduplicateFn[T any, C comparable](fn func(int, T) C, elems ...T) []T

DeduplicateFn[T, K] creates a copy of elems with duplicates removed based on the result of the fn(int, T) function

The fn(int, T) K functions takes:

  • int is the index of the current element
  • T is the current element
  • K the comparable type to deduplicate

func DeduplicateFnErr added in v1.0.33

func DeduplicateFnErr[T any, C comparable](fn func(int, T) (C, error), elems ...T) ([]T, error)

DedupFn[T, K] creates a copy of elems with duplicates removed based on the result of the fn(int, T) function

The fn(int, T) K functions takes:

  • int is the index of the current element
  • T is the current element
  • K the comparable type to deduplicate

If the fn function returns an error, processing through elems is stopped and the error is returned.

func Difference added in v1.0.33

func Difference[T comparable](as, bs []T) []T

func DifferenceFn added in v1.0.33

func DifferenceFn[T any, C comparable](fn func(int, T) C, as, bs []T) []T

func DifferenceFnErr added in v1.0.33

func DifferenceFnErr[T any, C comparable](fn func(int, T) (C, error), as, bs []T) ([]T, error)

func Duplicate added in v1.0.33

func Duplicate[T any](count int, elems ...T) []T

func Each

func Each[T any](fn func(int, T), elems ...T)

func EachErr

func EachErr[T any](fn func(int, T) error, elems ...T) error

func End added in v1.0.33

func End[T any](elems ...T) (T, bool)

func Equals

func Equals[T comparable](as, bs []T) bool

func EqualsFn

func EqualsFn[T any, C comparable](fn func(int, T) C, as, bs []T) bool

func EqualsFnErr

func EqualsFnErr[T any, C comparable](fn func(int, T) (C, error), as, bs []T) (bool, error)

func Exist

func Exist[T comparable](s T, elems ...T) bool

func ExistFn

func ExistFn[T any](fn func(int, T) bool, elems ...T) bool

func ExistFnErr

func ExistFnErr[T any](fn func(int, T) (bool, error), elems ...T) (bool, error)

func Filter

func Filter[T any](fn func(int, T) bool, elems ...T) []T

func FilterErr

func FilterErr[T any](fn func(int, T) (bool, error), elems ...T) ([]T, error)

func Find

func Find[T comparable](match T, elems ...T) (T, bool)

func FindFn

func FindFn[T any](fn func(int, T) bool, elems ...T) (T, bool)

func FindFnErr

func FindFnErr[T any](fn func(int, T) (bool, error), elems ...T) (T, bool, error)

func First added in v1.0.33

func First[T any](elems ...T) (T, bool)

func Flatten

func Flatten[T any](elems ...[]T) []T

func Fold added in v1.0.33

func Fold[T any, R any](fn func(int, R, T) R, initial R, elems ...T) R

func FoldErr added in v1.0.33

func FoldErr[T any, R any](fn func(int, R, T) (R, error), initial R, elems ...T) (R, error)

func FoldLeft

func FoldLeft[T any, R any](fn func(int, R, T) R, initial R, elems ...T) R

func FoldLeftErr

func FoldLeftErr[T any, R any](fn func(int, R, T) (R, error), initial R, elems ...T) (R, error)

func FoldRight

func FoldRight[T any, R any](fn func(int, R, T) R, initial R, elems ...T) R

func FoldRightErr

func FoldRightErr[T any, R any](fn func(int, R, T) (R, error), initial R, elems ...T) (R, error)

func Group added in v1.0.33

func Group[T any, C comparable](fn func(int, T) C, elems ...T) map[C][]T

Group creates a map composed of keys generated from the fn function and the value is the value passed to the fn function which genereated they key.

func GroupErr added in v1.0.33

func GroupErr[T any, C comparable](fn func(int, T) (C, error), elems ...T) (map[C][]T, error)

GroupErr creates a map composed of keys generated from the fn function and the value is the value passed to the fn function which genereated they key.

If the fn function returns an error, processing through elements is stopped and the error is returned. The values of the grouped elements will contain any elements processed up to but not including the element when the error occured.

func HasIntersection added in v1.0.33

func HasIntersection[T comparable](as []T, bs []T) bool

func HasIntersectionFn added in v1.0.33

func HasIntersectionFn[T any, C comparable](fn func(T) C, as []T, bs []T) bool

func HasIntersectionFnErr added in v1.0.33

func HasIntersectionFnErr[T any, C comparable](fn func(T) (C, error), as []T, bs []T) (bool, error)
func Head[T any](elems ...T) (T, bool)

func Intersection added in v1.0.33

func Intersection[T comparable](as []T, bs []T) []T

func IntersectionFn added in v1.0.33

func IntersectionFn[T any, C comparable](fn func(T) C, as []T, bs []T) []T

func IntersectionFnErr added in v1.0.33

func IntersectionFnErr[T any, C comparable](fn func(T) (C, error), as []T, bs []T) ([]T, error)

func Join

func Join[T any](sep string, elems ...T) string

func JoinErrFn

func JoinErrFn[T any](fn func(int, T) (string, error), sep string, elems ...T) (string, error)

func JoinFn

func JoinFn[T any](fn func(int, T) string, sep string, elems ...T) string

func Last

func Last[T any](elems ...T) (T, bool)

func Map

func Map[T any, R any](fn func(int, T) R, elems ...T) []R

func MapErr

func MapErr[T any, R any](fn func(int, T) (R, error), elems ...T) ([]R, error)

func MapPipe

func MapPipe[T any, R any](fns []func(int, T) R, elems ...T) []R

func MapPipeErr

func MapPipeErr[T any, R any](fns []func(int, T) (R, error), elems ...T) ([]R, error)

func Partition added in v1.0.33

func Partition[T any](fn func(int, T) bool, elems ...T) ([]T, []T)

PartitionErr splits the elements into two slices based on the result of the fn function.

The fn function takes the index of the current index of the element of elems, and the current value of the element in elems. It should return the computed compariable value extract from the element.

The first slice contains the elements for which the fn function returns a true value. The second slice contains the elements for which the fn function returns a false value.

for which the fn function returns a true value. The second slice contains the elems for which the fn fucntion returns a false value.

func PartitionErr added in v1.0.33

func PartitionErr[T any](fn func(int, T) (bool, error), elems ...T) ([]T, []T, error)

PartitionErr splits the elements into two slices based on the result of the fn function.

The fn function takes the index of the current index of the element of elems, and the current value of the element in elems. It should return the computed compariable value extract from the element or an error if computed compariable value can't be computed.

The first slice contains the elements for which the fn function returns a true value. The second slice contains the elements for which the fn function returns a false value.

If the fn function returns an error, processing through elements is stopped and the error is returned. The values of the partitions will contain any partitioned data up to but not including the value when the error occured.

func Prepend added in v1.0.33

func Prepend[T any](slice []T, elems ...T) []T

func Reduce

func Reduce[T any, R any](fn func(int, R, T) R, initial R, elems ...T) R

func ReduceErr

func ReduceErr[T any, R any](fn func(int, R, T) (R, error), initial R, elems ...T) (R, error)

func Reject added in v1.0.33

func Reject[T any](fn func(int, T) bool, elems ...T) []T

func RejectErr added in v1.0.33

func RejectErr[T any](fn func(int, T) (bool, error), elems ...T) ([]T, error)

func Reverse

func Reverse[T any](elems ...T) []T

func Sort

func Sort[T constraints.Ordered](elems ...T) []T

func SortFn added in v1.0.29

func SortFn[T any, O constraints.Ordered](fn func(T) O, elems ...T) []T

func SymmetricDifference added in v1.0.33

func SymmetricDifference[T comparable](as, bs []T) []T

func SymmetricDifferenceFn added in v1.0.33

func SymmetricDifferenceFn[T any, C comparable](fn func(int, T) C, as, bs []T) []T

func SymmetricDifferenceFnErr added in v1.0.33

func SymmetricDifferenceFnErr[T any, C comparable](fn func(int, T) (C, error), as, bs []T) ([]T, error)

func Tail

func Tail[T any](elems ...T) ([]T, bool)

func Times

func Times[T any](count int, elems ...T) []T

func Union added in v1.0.33

func Union[T comparable](as []T, bs []T) ([]T, error)

func UnionFn added in v1.0.33

func UnionFn[T any, C comparable](fn func(int, T) C, as []T, bs []T) ([]T, error)

func UnionFnErr added in v1.0.33

func UnionFnErr[T any, C comparable](fn func(int, T) (C, error), as []T, bs []T) ([]T, error)

func Uniq added in v1.0.32

func Uniq[C comparable](elems ...C) []C

func UniqFn added in v1.0.32

func UniqFn[T any, C comparable](fn func(int, T) C, elems ...T) []T

func UniqFnErr added in v1.0.33

func UniqFnErr[T any, C comparable](fn func(int, T) (C, error), elems ...T) ([]T, error)

Types

type CountDuplicatesResult added in v1.0.33

type CountDuplicatesResult[T any] struct {
	Count int
	Item  T
}

func CountDuplicates added in v1.0.33

func CountDuplicates[T comparable](elems ...T) []CountDuplicatesResult[T]

func CountDuplicatesFn added in v1.0.33

func CountDuplicatesFn[T any, C comparable](fn func(T) C, elems ...T) []CountDuplicatesResult[T]

func CountDuplicatesFnErr added in v1.0.33

func CountDuplicatesFnErr[T any, C comparable](fn func(T) (C, error), elems ...T) ([]CountDuplicatesResult[T], error)

type Pipe

type Pipe[T any] struct {
	// contains filtered or unexported fields
}

func NewPipe

func NewPipe[T any]() *Pipe[T]

func (*Pipe[T]) List

func (p *Pipe[T]) List(elems ...T) []T

func (*Pipe[T]) Single

func (p *Pipe[T]) Single(elem T) T

func (*Pipe[T]) Then

func (p *Pipe[T]) Then(fn func(int, T) T) *Pipe[T]

type PipeErr

type PipeErr[T any] struct {
	// contains filtered or unexported fields
}

func NewPipeErr

func NewPipeErr[T any]() *PipeErr[T]

func (*PipeErr[T]) List

func (p *PipeErr[T]) List(elems ...T) ([]T, error)

func (*PipeErr[T]) Single

func (p *PipeErr[T]) Single(elem T) (T, error)

func (*PipeErr[T]) Then

func (p *PipeErr[T]) Then(fn func(int, T) (T, error)) *PipeErr[T]

Jump to

Keyboard shortcuts

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