slice

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

Slice helpers

GoDev

Contains slice helpers.

Documentation

GoDoc

Documentation

Overview

Contains slice helpers (Map, Filter, Reduce, Fold, Remove, ToMap and ect.).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendNotNil added in v0.1.0

func AppendNotNil[T any](slice []*T, elems ...*T) []*T

Append not nil elements.

func Batch added in v0.1.0

func Batch[T any](s []T, size int, f func([]T) error) error

Batch split the slice to batches and call the callback function with the every batch.

Example
const size = 4
s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
var results [][]int
err := Batch(s, size, func(s []int) error {
	results = append(results, s)
	return nil
})

fmt.Println(err)
fmt.Println(results)

// with error
err = Batch(s, size, func(s []int) error {
	return errors.New("oops")
})

fmt.Println(err)

// empty
results = nil
_ = Batch(nil, size, func(s []int) error {
	results = append(results, s)
	return nil
})

fmt.Println(results)
Output:
<nil>
[[1 2 3 4] [5 6 7 8] [9]]
oops
[]

func Clone

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

Clone slice.

func Contains added in v0.1.0

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

Contains checks that the slice contains the specified value.

func ConvertNumbers added in v0.1.0

func ConvertNumbers[T, V constraints.Number](t []T) []V

ConvertNumbers converts slices of numbers of one number type to slices of another numbers type.

func Count added in v0.1.0

func Count[T any](s []T, f func(T) bool) int

Count values.

Example
fmt.Println(Count([]string{"fish", "crab", "", "octopus", "", "squid", "", ""}, func(v string) bool { return v == "" }))
Output:
4

func Diff

func Diff[T comparable](s1, s2 []T) []T

Diff returns s1 - s2.

func Each

func Each[T any](s []T, f func(v T))

Each iterates through map values.

func Equal added in v0.1.0

func Equal[T comparable](s1, s2 []T) bool

Equal compare two slices.

func Filter

func Filter[T any](s []T, f func(T) bool) []T

Filter filters values from a slice using a filter function. It returns a new slice with only the elements of s for which f returned true.

func FilterMap added in v0.1.0

func FilterMap[T1, T2 any](s []T1, filter func(T1) bool, mapper func(T1) T2) []T2

FilterMap first filters values from a slice using a filter function and then map them. It returns a new slice with only the mapped elements of s for which filter returned true.

func FindFirst added in v0.1.0

func FindFirst[T any](items []T, filter func(item T) bool) (T, bool)

FindFirst item.

func FitIndex added in v0.1.0

func FitIndex[T any](index int, s []T) int

FitIndex fits the index into slice range. If the slice is empty the result will be -1.

Example
fmt.Println(FitIndex(-1, []int{1, 1, 1}))
fmt.Println(FitIndex(11, []int{1, 1, 1}))
fmt.Println(FitIndex(1, []int{1, 1, 1}))
fmt.Println(FitIndex(1, []int{}))
Output:
0
2
1
-1

func Fold

func Fold[T comparable](s []T) []T

Fold deduplicates items.

func FoldFunc added in v0.1.0

func FoldFunc[T any, K comparable](s []T, key func(T) K) []T

FoldFunc deduplicates items by keys.

func FromMap added in v0.1.0

func FromMap[I any, K comparable, T any](items map[K]T, fn func(key K, value T) I) []I

FromMap extract slice from map.

func Group

func Group[T any, K comparable](s []T, key func(T) K) map[K][]T

Group items.

Example
type data struct {
	Key   int
	Value int
}

s := []data{{Key: 4, Value: 5}, {Key: 4, Value: 4}, {Key: 2, Value: 3}, {Key: 1, Value: 2}, {Key: 1, Value: 1}}
m := Group(s, func(v data) int { return v.Key })

fmt.Println(m[4])
fmt.Println(m[2])
fmt.Println(m[1])
Output:
[{4 5} {4 4}]
[{2 3}]
[{1 2} {1 1}]

func GroupOrder

func GroupOrder[T any, K comparable](s []T, key func(T) K, less func(s []T, i, j int) bool) map[K][]T

GroupOrder items.

Example
type data struct {
	Key   int
	Value int
}

s := []data{{Key: 4, Value: 5}, {Key: 4, Value: 4}, {Key: 2, Value: 3}, {Key: 1, Value: 2}, {Key: 1, Value: 1}}
m := GroupOrder(s, func(v data) int { return v.Key }, func(s []data, i, j int) bool { return s[i].Value < s[j].Value })

fmt.Println(m[4])
fmt.Println(m[2])
fmt.Println(m[1])
Output:
[{4 4} {4 5}]
[{2 3}]
[{1 1} {1 2}]

func Grow added in v0.1.0

func Grow[T any](s []T, capacity int) []T

Grow slice capacity.

func HasDuplicates added in v0.1.0

func HasDuplicates[T comparable](s []T) bool

HasDuplicates checks if the slice has deduplicates.

Example
fmt.Println(HasDuplicates([]int{1, 3, 4, 7, 0, 5, 100, 15, 30, 31}))
fmt.Println(HasDuplicates([]int{1, 3, 4, 7, 0, 5, 100, 3, 30, 31}))
fmt.Println(HasDuplicates([]int{1, 1}))
fmt.Println(HasDuplicates([]int{1}))
fmt.Println(HasDuplicates([]int{}))
fmt.Println(HasDuplicates([]int(nil)))
Output:
false
true
true
false
false
false

func HasDuplicatesFunc added in v0.1.0

func HasDuplicatesFunc[T, K comparable](s []T, key func(T) K) bool

HasDuplicatesFunc checks if the slice has deduplicates.

Example
fmt.Println(HasDuplicatesFunc([]int{1, 3, 4, 7, 0, 5, 100, 15, 30, 31}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int{1, 3, 4, 7, 0, 5, 100, 3, 30, 31}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int{1, 1}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int{1}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int{}, func(i int) int { return i }))
fmt.Println(HasDuplicatesFunc([]int(nil), func(i int) int { return i }))
Output:
false
true
true
false
false
false

func Index

func Index[T comparable](s []T, v T) int

Index returns the index of the first instance of v in s, or -1 if v is not present in s.

func Insert

func Insert[T any](s []T, pos int, v ...T) []T

Insert the value at the specified position of the slice.

func Intersect

func Intersect[T comparable](s1, s2 []T) []T

Intersect returns m1 values that keys is contained in m2.

func Last added in v0.1.3

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

Last returns last element. It panics if slice has zero length.

Example
fmt.Println(Last([]int{10, 20, 30}))
Output:
30

func LastExists added in v0.1.3

func LastExists[T constraints.Ordered](s []T) (element T, exists bool)

LastExists returns last element.

Example
fmt.Println(LastExists([]int{10, 20, 30}))
fmt.Println(LastExists([]int{}))
fmt.Println(LastExists[int](nil))
Output:
30 true
0 false
0 false

func Map

func Map[T1, T2 any](s []T1, f func(T1) T2) []T2

Map turns a []T1 to a []T2 using a mapping function. This function has two type parameters, T1 and T2. This works with slices of any type.

func MapFilter added in v0.1.0

func MapFilter[T1, T2 any](s []T1, f func(T1) (T2, bool)) []T2

MapFilter turns a []T1 to a []T2 using a mapping function, Values is not placed to result slice when the mapping function return false. The resulting slice may have a smaller size than the original.

func MapFold added in v0.1.11

func MapFold[T any, K comparable](ss []T, f func(T) K) []K

MapFold turns a []T to a []K using a mapping function. The result will be contain unique values.

Example
fmt.Println(MapFold([]int{0, 1, 2, 3, 4, 5}, strconv.Itoa))
fmt.Println(MapFold([]int{0, 1, 2, 1, 3, 4, 5, 0}, strconv.Itoa))
fmt.Println(MapFold(nil, strconv.Itoa))
Output:
[0 1 2 3 4 5]
[0 1 2 3 4 5]
[]

func MapIndexed added in v0.1.0

func MapIndexed[T1, T2 any](s []T1, f func(int, T1) T2) []T2

MapIndexed turns a []T1 to a []T2 using a mapping function. This function has two type parameters, T1 and T2. This works with slices of any type.

func MapNotNil added in v0.1.0

func MapNotNil[T1, T2 any](s []*T1, f func(*T1) T2) []T2

MapNotNil turns a []*T1 to a []T2 using a mapping function, exclude nil values. This works with slices of any type. The resulting slice may have a smaller size than the original.

func Max added in v0.0.2

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

Max returns the maximum value of the slice.

Example
fmt.Println(Max(10, 0, 1))
fmt.Println(Max(1))
Output:
10
1

func Merge added in v0.1.0

func Merge[T any](ss ...[]T) []T

Merge slices into one.

Example
fmt.Println(Merge([]int{1, 2, 3, 4}, []int{4, 5, 6}, nil, []int{5, 7}))
Output:
[1 2 3 4 4 5 6 5 7]

func MergeSorted added in v0.1.0

func MergeSorted[T any](s1 []T, s2 []T, less func(v1, v2 T) bool, limit int) []T

MergeSorted merges two sorted slices to one new sorted. If limit is zero then result size is not limited.

Example
fmt.Println(MergeSorted([]int{1, 3, 4, 7}, []int{2, 3, 6, 8}, func(v1, v2 int) bool { return v1 < v2 }, 0))
fmt.Println(MergeSorted([]int{1, 3, 4, 7}, []int{2, 3, 6, 8}, func(v1, v2 int) bool { return v1 < v2 }, 3))
Output:
[1 2 3 3 4 6 7 8]
[1 2 3]

func MergeSortedTo added in v0.1.0

func MergeSortedTo[T any](dst, src1, src2 []T, less func(v1, v2 T) bool, limit int) []T

MergeSortedTo merges two sorted slices to one sorted. Dst may be overwritten if capacity is enough. s1 or s2 may be used as dst (for collecting values from many slices to one). If limit is zero then result size is not limited.

Example
s1, s2 := []int{1, 3, 4, 7}, []int{2, 3, 6, 8}
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 0))
s1, s2 = []int{1, 3, 4, 7}, []int{2, 3, 6, 8}
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 3))
s1, s2 = []int{1, 3, 4, 7}, []int{2, 3, 6, 8}
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 20))
s1, s2 = nil, []int{2, 3, 6, 8}
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 0))
s1, s2 = []int{2, 3, 6, 8}, nil
fmt.Println(MergeSortedTo(s1, s1, s2, func(v1, v2 int) bool { return v1 < v2 }, 0))
Output:
[1 2 3 3 4 6 7 8]
[1 2 3]
[1 2 3 3 4 6 7 8]
[2 3 6 8]
[2 3 6 8]

func Min added in v0.0.2

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

Min returns the minimum value of the slice.

Example
fmt.Println(Min(10, 0, 1))
fmt.Println(Min(1))
Output:
0
1

func New added in v0.1.0

func New[T any](size int) []*T

New allocate fast slice of pointers of specified type.

func NewFrom added in v0.1.0

func NewFrom[K, T any](source []K, init func(dst *T, src K)) []*T

NewFrom allocate fast the slice of pointers of specified type and initializes it.

func NewFromFilter added in v0.1.0

func NewFromFilter[K, T any](source []K, init func(dst *T, src K) bool) []*T

NewFromFilter allocate fast the slice of pointers of specified type and initializes it. It skips output if the init function return false.

func NewInit added in v0.1.0

func NewInit[T any](size int, init func(i int, item *T)) []*T

NewInit allocate fast the slice of pointers of specified type and initializes it.

func NewInitFilter added in v0.1.0

func NewInitFilter[T any](size int, init func(i int, item *T) bool) []*T

NewInitFilter allocate fast the slice of pointers of specified type and initializes it.

func ProcessNotNil added in v0.1.0

func ProcessNotNil[T any](s []*T, f func(*T) error) error

ProcessNotNil process not nil elements.

func Reduce

func Reduce[T, R any](s []T, initializer R, f func(R, T) R) R

Reduce reduces a []T to a single value using a reduction function.

func Remove added in v0.1.0

func Remove[T any](s []T, pos int) []T

Remove the element at position. It returns the same slice with reduced size.

func RemoveFunc added in v0.1.0

func RemoveFunc[T any](s []T, remove func(item T) bool) []T

Remove elements from the slice. It returns the same slice with reduced size.

Example
fmt.Println(RemoveFunc([]int{2, 3, 4, 7, 0, 5, 100, 15, 30, 31}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int{1, 3, 4, 7, 0, 5, 100, 15, 30, 32}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int{1, 1}, func(i int) bool { return i%2 == 1 }))
fmt.Println(RemoveFunc([]int{1, 1}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int{1}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int{1}, func(i int) bool { return i%2 == 1 }))
fmt.Println(RemoveFunc([]int{}, func(i int) bool { return i%2 == 0 }))
fmt.Println(RemoveFunc([]int(nil), func(i int) bool { return i%2 == 0 }))
Output:
[3 7 5 15 31]
[1 3 7 5 15]
[]
[1 1]
[1]
[]
[]
[]

func RemoveMany added in v0.1.0

func RemoveMany[T any](s []T, pos int, amount int) []T

Remove the elements at position. It returns the same slice with reduced size.

func Reverse

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

Reverse items of the slice.

func Sort added in v0.1.0

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

Sort slices ascending.

Example
s := []int{10, 120, 30}
Sort(s)
fmt.Println(s)
Output:
[10 30 120]

func SortDesc added in v0.1.0

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

Sort slices descending.

Example
s := []int{10, 120, 30}
SortDesc(s)
fmt.Println(s)
Output:
[120 30 10]

func SymmetricDiff

func SymmetricDiff[T comparable](s1, s2 []T) []T

SymmetricDiff gets the symmetric difference of two sets and gives a set of elements, which are in either of the sets and not in their intersection.

func ToMap added in v0.1.0

func ToMap[I any, K comparable, T any](items []I, fn func(item I) (K, T)) map[K]T

ToMap convert slice to map.

func Union added in v0.1.0

func Union[T comparable](ss ...[]T) []T

Union slices into one. Duplicates is removed. Order of items in slices is preserved.

Example
fmt.Println(Union([]int{1, 2, 3, 4}, []int{4, 5, 6}, []int{5, 7}))
fmt.Println(Union([]int{4, 5, 6}, []int{1, 2, 3, 4}, []int{5, 7}))
fmt.Println(Union(nil, []int{4, 5, 6}, nil))
fmt.Println(Union([]string(nil)))
Output:
[1 2 3 4 5 6 7]
[4 5 6 1 2 3 7]
[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