slice

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[T any](src []T, index int, item T) ([]T, error)

Add insert an item at the specified index in the slice.

func Contains

func Contains[T comparable](slice []T, elem T) bool

Contains checks if the slice contains the given element.

func ContainsAll

func ContainsAll[T comparable](slice []T, elems []T) bool

ContainsAll checks if the slice contains all of the given elements.

func ContainsAllFunc

func ContainsAllFunc[T comparable](slice []T, elems []T, eq eqFunc[T]) bool

ContainsAllFunc checks if the slice contains all of the given elements.

func ContainsAny

func ContainsAny[T comparable](slice []T, elems []T) bool

ContainsAny checks if the slice contains any of the given elements.

func ContainsAnyFunc

func ContainsAnyFunc[T comparable](slice []T, elems []T, eq eqFunc[T]) bool

ContainsAnyFunc checks if the slice contains any of the given elements.

func ContainsFunc

func ContainsFunc[T comparable](slice []T, eq func(t T) bool) bool

ContainsFunc checks if the slice contains an element that satisfies the given function.

func Del

func Del[T any](src []T, index int) ([]T, error)

Del removes an element at the specified index from the slice.

func DiffSet

func DiffSet[T comparable](src, dst []T) []T

DiffSet returns the elements in src that are not in dst.

Example
src := []int{1, 2, 3, 4, 5}
dst := []int{2, 3, 4, 6, 7}
diff := DiffSet(src, dst)
sort.Ints(diff)
fmt.Println(diff)
Output:

[1 5]

func DiffSetFunc

func DiffSetFunc[T comparable](src, dst []T, eq eqFunc[T]) []T
Example
src := []int{1, 2, 3, 4, 5}
dst := []int{2, 3, 4, 6, 7}
eq := func(a, b int) bool { return a == b }
diff := DiffSetFunc(src, dst, eq)
fmt.Println(diff)
Output:

[1 5]

func FilterDel

func FilterDel[T any](src []T, filter func(idx int, elem T) bool) []T

FilterDel removes elements from the slice that match the filter function.

func FilterMap

func FilterMap[Src any, Dst any](src []Src, filter func(idx int, src Src) (Dst, bool)) []Dst

FilterMap filters and maps a slice using a function.

Example
src := []int{1, 2, 3, 4, 5}
res := FilterMap(src, func(idx int, src int) (int, bool) { return src * 2, idx%2 == 0 })
fmt.Println(res)
Output:

[2 6 10]

func Find

func Find[T any](slice []T, match matchFunc[T]) (T, bool)

Find returns the first element that matches the condition.

Example
slice := []int{1, 2, 3, 4, 5}
res, ok := Find(slice, mf)
fmt.Println(res, ok)
Output:

2 true

func FindAll

func FindAll[T any](slice []T, match matchFunc[T]) []T

FindAll returns all elements that match the condition.

Example
slice := []int{1, 2, 3, 4, 5}
res := FindAll(slice, mf)
fmt.Println(res)
Output:

[2 4]

func Index

func Index[T comparable](slice []T, elem T) int

Index returns the index of the first occurrence of elem in slice.

Example
slice := []int{1, 2, 3, 2, 4}
res := Index(slice, 2)
fmt.Println(res)
Output:

1

func IndexAll

func IndexAll[T comparable](slice []T, elem T) []int

IndexAll returns all indices of elements that match the condition.

Example
slice := []int{1, 2, 3, 2, 4}
res := IndexAll(slice, 2)
fmt.Println(res)
Output:

[1 3]

func IndexAllFunc

func IndexAllFunc[T comparable](slice []T, match matchFunc[T]) []int

IndexAllFunc returns all indices of elements that match the condition.

Example
slice := []int{1, 2, 3, 2, 4}
res := IndexAllFunc(slice, func(t int) bool { return t == 2 })
fmt.Println(res)
Output:

[1 3]

func IndexFunc

func IndexFunc[T comparable](slice []T, match matchFunc[T]) int

IndexFunc returns the index of the first element that matches the condition.

Example
slice := []int{1, 2, 3, 2, 4}
res := IndexFunc(slice, func(t int) bool { return t == 2 })
fmt.Println(res)
Output:

1

func IntersectSet

func IntersectSet[T comparable](src, dst []T) []T

IntersectSet returns the intersection of two slices.

Example
src := []int{1, 2, 3, 4, 5}
dst := []int{4, 5, 6, 7, 8}
res := IntersectSet(src, dst)
fmt.Println(res)
Output:

[4 5]

func IntersectSetFunc

func IntersectSetFunc[T comparable](src, dst []T, eq eqFunc[T]) []T

IntersectSetFunc returns the intersection of two slices.

Example
src := []int{1, 2, 3, 4, 5}
dst := []int{4, 5, 6, 7, 8}
res := IntersectSetFunc(src, dst, func(src, dst int) bool { return src == dst })
fmt.Println(res)
Output:

[4 5]

func LastIndex

func LastIndex[T comparable](slice []T, elem T) int

LastIndex returns the index of the last occurrence of elem in slice.

Example
slice := []int{1, 2, 3, 2, 4}
res := LastIndex(slice, 2)
fmt.Println(res)
Output:

3

func LastIndexFunc

func LastIndexFunc[T comparable](slice []T, match matchFunc[T]) int

LastIndexFunc returns the index of the last element that matches the condition.

Example
slice := []int{1, 2, 3, 2, 4}
res := LastIndexFunc(slice, func(t int) bool { return t == 2 })
fmt.Println(res)
Output:

3

func Map

func Map[Src any, Dst any](src []Src, fn func(idx int, src Src) Dst) []Dst

Map maps a slice to a new slice using a function.

Example
src := []int{1, 2, 3, 4, 5}
res := Map(src, func(idx int, src int) int { return src * 2 })
fmt.Println(res)
Output:

[2 4 6 8 10]

func Max

func Max[T easykit.RealNumber](slice []T) (T, error)

Max returns the maximum value in the slice.

Example
slice := []int{1, 2, 3, 4, 5}
m, _ := Max(slice)
fmt.Println(m)
Output:

5

func Min

func Min[T easykit.RealNumber](slice []T) (T, error)

Min returns the minimum value in the slice.

Example
slice := []int{1, 2, 3, 4, 5}
m, _ := Min(slice)
fmt.Println(m)
Output:

1

func Reverse

func Reverse[T any](slice []T) []T

Reverse reverses a slice in place.

Example
res1 := Reverse([]int{1, 2, 3, 2, 4})
fmt.Println(res1)

res2 := Reverse([]string{"a", "b", "c"})
fmt.Println(res2)
Output:

[4 2 3 2 1]
[c b a]

func ReverseInPlace

func ReverseInPlace[T any](slice []T)

ReverseInPlace reverses a slice in place.

Example
s1 := []int{1, 2, 3, 2, 4}
ReverseInPlace(s1)
fmt.Println(s1)

s2 := []string{"a", "b", "c", "g", "d", "e"}
ReverseInPlace(s2)
fmt.Println(s2)
Output:

[4 2 3 2 1]
[e d g c b a]

func Sum

func Sum[T easykit.RealNumber](slice []T) T

Sum returns the sum of the slice.

Example
slice := []int{1, 2, 3, 4, 5}
sum := Sum(slice)
fmt.Println(sum)
Output:

15

func SymmDiffSet

func SymmDiffSet[T comparable](src []T, dst []T) []T

SymmDiffSet symmetric difference of two slices

Example
src := []int{1, 2, 3, 4, 5}
dst := []int{4, 5, 6, 7, 8}
res := SymmDiffSet(src, dst)
sort.Ints(res)
fmt.Println(res)
Output:

[1 2 3 6 7 8]

func SymmDiffSetFunc

func SymmDiffSetFunc[T comparable](src []T, dst []T, eq eqFunc[T]) []T

SymmDiffSetFunc symmetric difference of two slices

Example
src := []int{1, 2, 3, 4, 5}
dst := []int{4, 5, 6, 7, 8}
res := SymmDiffSetFunc(src, dst, func(a, b int) bool { return a == b })
fmt.Println(res)
Output:

[1 2 3 6 7 8]

func ToMap

func ToMap[K comparable, V any](slice []V, fn func(elem V) K) map[K]V

ToMap converts a slice to a map. the key is the result of the function.

func ToMapWithVal

func ToMapWithVal[E any, K comparable, V any](slice []E, fn func(elem E) (K, V)) map[K]V

ToMapWithVal converts a slice to a map. the key and value are the result of the function.

func UnionSet

func UnionSet[T comparable](src, dst []T) []T

UnionSet returns the union of two sets.

Example
src := []int{1, 2, 3}
dst := []int{4, 5, 6}
res := UnionSet(src, dst)
sort.Ints(res)
fmt.Println(res)
Output:

[1 2 3 4 5 6]

func UnionSetFunc

func UnionSetFunc[T comparable](src, dst []T, eq eqFunc[T]) []T

UnionSetFunc returns the union of two sets.

Example
src := []int{1, 2, 3}
dst := []int{4, 5, 6}
res := UnionSetFunc(src, dst, func(src, dst int) bool { return src == dst })
fmt.Println(res)
Output:

[1 2 3 4 5 6]

Types

This section is empty.

Jump to

Keyboard shortcuts

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