slicex

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: BSD-2-Clause Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InvalidIndexErr = errors.New("invalid index")

InvalidIndexErr slice index invalid

Functions

func Append

func Append[T any](slice []T, v T) []T

Append add a new value to the end of slice.

func Concat

func Concat[T any](s1 []T, s2 []T, others ...[]T) []T

Concat concat multi slices into one slice

func Contains

func Contains[T comparable](s []T, value T) bool

Contains return whether slice contains the given value

func Convert

func Convert[T any, R any](s []T, convert func(v T) R) []R

Convert apply convert func on the origin slice, return a new slice contains the generated value.

func Copy

func Copy[T any](s []T) []T

Copy copies a slice. copy can also trim slice underlying array to it's len.

func Find

func Find[T comparable](s []T, value T) int

Find return the index of value; if not found, return -1

func FindBy

func FindBy[T any](s []T, predicate func(v T) bool) int

FindBy find the value match the predicate, return the index; if not found, return -1

func FindLast

func FindLast[T comparable](s []T, value T) int

FindLast return the last index of lvalue; if not found, return -1

func First

func First[T any](s []T) option.Option[T]

First return the first element of slice in a gox.Option, return empty option if slice is empty.

func Insert

func Insert[T any](slice []T, index int, value T) []T

Insert inserts new value at the position of slice. The index value must in range [0, len(slice)]

func Last

func Last[T any](s []T) option.Option[T]

Last return the last element of slice in a gox.Option, return empty option if slice is empty.

func Max

func Max[T constraints.Ordered](s []T) option.Option[T]

Max returns an Option contains the max value in the slice. If slice is empty, return empty Option.

func MaxBy

func MaxBy[T any](s []T, less func(v1, v2 T) bool) option.Option[T]

MaxBy returns an Option contains the max value in the slice. If slice is empty, return empty Option. The values are compared by less func.

func Min

func Min[T constraints.Ordered](s []T) option.Option[T]

Min returns an Option contains the min value in the slice. If slice is empty, return empty Option.

func MinBy

func MinBy[T any](s []T, less func(v1, v2 T) bool) option.Option[T]

MinBy returns an Option contains the min value in the slice. If slice is empty, return empty Option. The values are compared by less func.

func Partition

func Partition[T any](s []T, predicate func(v T) bool) (matched []T, mismatched []T)

Partition divide slice into two slice, one contains elements match the predicate, the other contains the mismatched.

func Prepend

func Prepend[T any](slice []T, v T) []T

Prepend add a new value to the beginning of slice.

func Reduce

func Reduce[T any, R any](s []T, initial R, reducer func(current R, value T) R) R

Reduce returns max value in the slice. If slice is empty, return zero value.

func Remove

func Remove[T comparable](slice []T, value T) []T

Remove removes the value match the given condition predicate.

func RemoveAt

func RemoveAt[T any](slice []T, index int) []T

RemoveAt remove the value at the position of slice. The index value must in range [0, len(slice)-1]

func RemoveBy

func RemoveBy[T any](slice []T, predicate func(value T) bool) []T

RemoveBy remove the value match the given condition predicate.

func Select

func Select[T any](s []T, predicate func(v T) bool) []T

Select apply predicate on the origin slice, return a new slice contains the values match the predicate.

func Sort

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

Sort sorts the slice inplace

func SortBy

func SortBy[T any](s []T, less func(v1, v2 T) bool)

SortBy sorts the slice inplace, a less function is specified to compare slice values.

func SortStable

func SortStable[T constraints.Ordered](s []T)

SortStable sorts the slice inplace, with stable sort algorithm

func SortStableBy

func SortStableBy[T any](s []T, less func(v1, v2 T) bool)

SortStableBy sorts the slice inplace, with stable sort algorithm. A less function is specified to compare slice values.

func Sorted

func Sorted[T constraints.Ordered](s []T) []T

Sorted a new slice which contains elements from origin slice, and is sorted.

Types

type Slice added in v2.3.0

type Slice[T any] []T

Slice type, for adding convinent methods to slice

func New added in v2.2.0

func New[T any](values ...T) Slice[T]

New create a new new slice, with optinal values If no values are passed, return a nil slice

func (*Slice[T]) Append added in v2.3.0

func (s *Slice[T]) Append(v T)

Append add a new value to the end of slice, and update the slice.

func (*Slice[T]) AppendAll added in v2.3.0

func (s *Slice[T]) AppendAll(vs ...T)

AppendAll add values to the end of slice, and update the slice.

func (Slice[T]) Copy added in v2.3.0

func (s Slice[T]) Copy() []T

Copy copies a slice. copy can also trim slice underlying array to it's len.

func (Slice[T]) FindBy added in v2.3.0

func (s Slice[T]) FindBy(predicate func(v T) bool) int

FindBy find the value match the predicate, return the index; if not found, return -1

func (Slice[T]) First added in v2.3.0

func (s Slice[T]) First() option.Option[T]

First return the first element of slice in a gox.Option, return empty option if slice is empty.

func (*Slice[T]) Insert added in v2.3.0

func (s *Slice[T]) Insert(index int, value T)

Insert inserts new value at the position of slice, and update the slice. The index value must in range [0, len(slice)]

func (Slice[T]) IsEmpty added in v2.3.0

func (s Slice[T]) IsEmpty() bool

IsEmpty return if Slice is empty

func (Slice[T]) Last added in v2.3.0

func (s Slice[T]) Last() option.Option[T]

Last return the last element of slice in a gox.Option, return empty option if slice is empty.

func (Slice[T]) Len added in v2.3.0

func (s Slice[T]) Len() int

Len return len of Slice

func (Slice[T]) MaxBy added in v2.3.0

func (s Slice[T]) MaxBy(less func(v1, v2 T) bool) option.Option[T]

MaxBy returns an Option contains the max value in the slice. If slice is empty, return empty Option. The values are compared by less func.

func (Slice[T]) MinBy added in v2.3.0

func (s Slice[T]) MinBy(less func(v1, v2 T) bool) option.Option[T]

MinBy returns an Option contains the min value in the slice. If slice is empty, return empty Option. The values are compared by less func.

func (Slice[T]) Partition added in v2.3.0

func (s Slice[T]) Partition(predicate func(v T) bool) (matched []T, mismatched []T)

Partition divide slice into two slice, one contains elements match the predicate, the other contains the mismatched.

func (*Slice[T]) Prepend added in v2.3.0

func (s *Slice[T]) Prepend(v T)

Prepend add a new value to the beginning of slice, and update the slice.

func (*Slice[T]) PrependAll added in v2.3.0

func (s *Slice[T]) PrependAll(vs ...T)

PrependAll new values to the beginning of slice, and update the slice.

func (*Slice[T]) RemoveAt added in v2.3.0

func (s *Slice[T]) RemoveAt(index int)

RemoveAt remove the value at the position of slice, and update the slice . The index value must in range [0, len(slice)-1]

func (*Slice[T]) RemoveBy added in v2.3.0

func (s *Slice[T]) RemoveBy(predicate func(value T) bool)

RemoveBy remove the value match the given condition predicate, and update the slice inplce.

func (Slice[T]) Select added in v2.3.0

func (s Slice[T]) Select(predicate func(v T) bool) []T

Select apply predicate on the origin slice, return a new slice contains the values match the predicate.

func (Slice[T]) SortBy added in v2.3.0

func (s Slice[T]) SortBy(less func(v1, v2 T) bool)

SortBy sorts the slice inplace, a less function is specified to compare slice values.

func (Slice[T]) SortStableBy added in v2.3.0

func (s Slice[T]) SortStableBy(less func(v1, v2 T) bool)

SortStableBy sorts the slice inplace, with stable sort algorithm. A less function is specified to compare slice values.

func (Slice[T]) Sub added in v2.3.0

func (s Slice[T]) Sub(begin, end int) Slice[T]

Slice return sub slice. param begin, end: the begin and end index of sub slice. Negetive value mean count from last.

Jump to

Keyboard shortcuts

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