Documentation
¶
Overview ¶
Package gslice provides some useful functions for slice.
Index ¶
- func All[T any](in []T, f func(T) bool) bool
- func Any[T any](in []T, f func(T) bool) bool
- func Avg[T constraints.Number](in []T) float64
- func AvgBy[V any, T constraints.Number](in []V, f func(V) T) float64
- func AvgN[T constraints.Number](inputs ...T) float64
- func Chunk[T any, Slice ~[]T](in Slice, chunkSize int) []Slice
- func ChunkInPlace[T any, Slice ~[]T](in Slice, chunkSize int) []Slice
- func Clone[T any](in []T) []T
- func CloneBy[T any, U any](in []T, f func(T) U) []U
- func Concat[T any](vs ...[]T) []T
- func Contains[T comparable](in []T, v T) bool
- func ContainsAll[T comparable](in []T, v []T) bool
- func ContainsAny[T comparable](in []T, v []T) bool
- func ContainsBy[T any](in []T, f func(T) bool) bool
- func Count[T any](in []T) int
- func Find[T any](in []T, f func(T) bool) (val T, found bool)
- func FindO[T any](in []T, f func(T) bool) optional.O[T]
- func ForEach[T any](in []T, f func(T) bool)
- func ForEachIdx[T any](in []T, f func(idx int, v T) bool)
- func Head[T any](in []T) (v T, hasOne bool)
- func HeadO[T any](in []T) optional.O[T]
- func Join[T ~string](in []T, sep T) T
- func Map[T any, U any](in []T, f func(T) U) []U
- func Max[T constraints.Ordered](in []T) optional.O[T]
- func MaxBy[T constraints.Ordered](in []T, f func(T, T) bool) optional.O[T]
- func MaxN[T constraints.Ordered](in ...T) optional.O[T]
- func Min[T constraints.Ordered](in []T) optional.O[T]
- func MinBy[T constraints.Ordered](in []T, f func(T, T) bool) optional.O[T]
- func MinN[T constraints.Ordered](in ...T) optional.O[T]
- func Repeat[T any, Slice ~[]T](in Slice, count int) Slice
- func RepeatBy[T any](n int, f func(i int) T) []T
- func Replace[T comparable, Slice ~[]T](in Slice, from, to T, count int) []T
- func ReplaceAll[T comparable, Slice ~[]T](in Slice, from, to T) []T
- func Reverse[T any, Slice ~[]T](in Slice)
- func ReverseClone[T any, Slice ~[]T](in Slice) Slice
- func Shuffle[T any, Slice ~[]T](in Slice) Slice
- func ShuffleInPlace[T any, Slice ~[]T](in Slice)
- func Subset[T any, Slice ~[]T](in Slice, start, count int) Slice
- func SubsetInPlace[T any, Slice ~[]T](in Slice, start int, count uint) Slice
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if all elements in the slice satisfy the condition provided by f. return false if any element in the slice does not satisfy the condition provided by f.
EXAMPLE:
gslice.All([]int{1, 2, 3}, func(x int) bool { return x > 0 }) 👉 true
gslice.All([]int{-1, 1, 2, 3}, func(x int) bool { return x > 0 }) 👉 false
func Any ¶
Any returns true if any element in the slice satisfy the condition provided by f. return false if none of element in the slice satisfy the condition provided by f.
EXAMPLE:
gslice.Any([]int{0, 1, 2, 3}, func(x int) bool { return x == 0 }) 👉 true
gslice.Any([]int{0, 1, 2, 3}, func(x int) bool { return x == -1 }) 👉 false
func Avg ¶
func Avg[T constraints.Number](in []T) float64
Avg returns the average value of the items in slice (float64).
EXAMPLE:
gslice.Avg([]int{1, 2, 3}) 👉 float(2)
gslice.Avg([]int{}) 👉 float(0)
func AvgBy ¶
func AvgBy[V any, T constraints.Number](in []V, f func(V) T) float64
AvgBy returns the averaged of each item's value evaluated by f.
EXAMPLE:
gslice.AvgBy([]string{"1", "2", "3"}, func(x string) int {
i, _ := strconv.Atoi(x)
return i
}) 👉 float(2)
func AvgN ¶
func AvgN[T constraints.Number](inputs ...T) float64
AvgN returns the average value of the items
EXAMPLE:
gslice.AvgN(1, 2, 3) 👉 float(2) gslice.AvgN() 👉 float(0)
func Chunk ¶ added in v0.1.5
Chunk returns a new slice with the elements in the slice chunked into smaller slices of the specified size.
EXAMPLE:
gslice.Chunk([]int{1, 2, 3, 4, 5}, 2) 👉 [[1, 2], [3, 4], [5]]
gslice.Chunk([]int{1, 2, 3, 4, 5}, 10) 👉 [[1, 2, 3, 4, 5]]
gslice.Chunk([]int{1, 2, 3, 4, 5}, 0) 👉 []int{}
func ChunkInPlace ¶ added in v0.1.5
ChunkInPlace returns a new slice with the elements in the slice chunked into smaller slices of the specified size. This function will not copy the elements, has no extra costs. EXAMPLE:
gslice.Chunk([]int{1, 2, 3, 4, 5}, 2) 👉 [[1, 2], [3, 4], [5]]
gslice.Chunk([]int{1, 2, 3, 4, 5}, 10) 👉 [[1, 2, 3, 4, 5]]
gslice.Chunk([]int{1, 2, 3, 4, 5}, 0) 👉 []int{}
func Clone ¶
func Clone[T any](in []T) []T
Clone returns a copy of the slice.
EXAMPLE:
gslice.Clone([]int{1, 2, 3}) 👉 [1, 2, 3]
func CloneBy ¶
CloneBy returns a copy of the slice with the results of applying the given function to every element in this slice.
EXAMPLE:
gslice.CloneBy([]int{1, 2, 3}, func(x int) int { return x * 2 }) 👉 [2, 4, 6]
gslice.CloneBy([]int{1, 2, 3}, strconv.Itoa) 👉 ["1", "2", "3"]
func Concat ¶
func Concat[T any](vs ...[]T) []T
Concat concatenates the slices.
EXAMPLE:
gslice.Concat([]int{1, 2, 3}, []int{4, 5, 6}) 👉 [1, 2, 3, 4, 5, 6]
gslice.Concat([]int{1, 2, 3}, []int{}) 👉 [1, 2, 3]
func Contains ¶
func Contains[T comparable](in []T, v T) bool
Contains returns true if the slice contains the value v.
EXAMPLE:
gslice.Contains([]int{1, 2, 3}, 1) 👉 true
gslice.Contains([]int{-1, 2, 3}, 1) 👉 false
func ContainsAll ¶
func ContainsAll[T comparable](in []T, v []T) bool
ContainsAll returns true if the slice contains all values in v.
EXAMPLE:
gslice.ContainsAll([]string{"1", "2", "3"}, []string{"1", "2", "3"}) 👉 true
gslice.ContainsAll([]string{"1", "2", "3"}, []string{"1", "99", "1000"}) 👉 false
gslice.ContainsAll([]string{"1", "2", "3"}, []string{}) 👉 true
func ContainsAny ¶
func ContainsAny[T comparable](in []T, v []T) bool
ContainsAny returns true if the slice contains any value in v.
EXAMPLE:
gslice.ContainsAny([]string{"1", "2", "3"}, []string{"1", "99", "1000"}) 👉 true
gslice.ContainsAny([]string{"1", "2", "3"}, []string{"-1"}) 👉 false
gslice.ContainsAny([]string{"1", "2", "3"}, []string{}) 👉 false
func ContainsBy ¶
ContainsBy returns true if the slice contains the value v evaluated by f.
EXAMPLE:
gslice.ContainsBy([]string{"1", "2", "3"}, func(x string) bool {
i, _ := strconv.Atoi(x)
return i == 1
}) 👉 true
gslice.ContainsBy([]string{"1", "2", "3"}, func(x string) bool {
i, _ := strconv.Atoi(x)
return i == -1
}) 👉 false
func Count ¶
Count returns the number of items in the slice.
EXAMPLE:
gslice.Count([]int{1, 2, 3}) 👉 3
gslice.Count([]int{}) 👉 0
func Find ¶
Find returns the first item in the slice that satisfies the condition provided by f.
EXAMPLE:
gslice.Find([]int{1, 2, 3}, func(x int) bool { return x == 1 }) 👉 1, true
gslice.Find([]int{1, 2, 3}, func(x int) bool { return x == -1 }) 👉 0, false
func FindO ¶
FindO returns the first item in the slice that satisfies the condition provided by f.
EXAMPLE:
gslice.FindO(_range(0, 10), func(x int) bool { return x == 1 }).Must() 👉 1
gslice.FindO(_range(0, 10), func(x int) bool { return x == -1 }).Ok() 👉 false
func ForEach ¶
ForEach iterates over each item in the slice, stop if f returns false.
EXAMPLE:
ForEach([]int{1, 2, 3}, func(x int) bool {
fmt.Println(x)
return true
}
Output:
1
2
3
func ForEachIdx ¶
ForEachIdx iterates over each item in the slice, stop if f returns false.
EXAMPLE:
ForEach([]int{1, 2, 3}, func(idx, x int) bool {
fmt.Println(idx, x)
return true
}
Output:
0 1
1 2
2 3
func Head ¶
Head returns the first item in the slice.
EXAMPLE:
optional.FromValue2(gslice.Head(_range(0, 10))).Must() 👉 0 optional.FromValue2(gslice.Head(_range(0, 0))).Ok() 👉 false
func HeadO ¶
HeadO returns the first item in the slice.
EXAMPLE:
gslice.HeadO(_range(0, 10)).Must() 👉 0 gslice.HeadO(_range(0, 0)).Ok() 👉 false
func Join ¶
func Join[T ~string](in []T, sep T) T
Join joins the slice with sep.
EXAMPLE:
gslice.Join([]string{"1", "2", "3"}, ".") 👉 "1.2.3"
gslice.Join([]string{}, ".") 👉 ""
func Map ¶
Map returns a new slice with the results of applying the given function to every element in this slice.
EXAMPLE:
gslice.Map([]int{1, 2, 3}, func(x int) int { return x * 2 }) 👉 [2, 4, 6]
gslice.Map([]int{1, 2, 3}, strconv.Itoa) 👉 ["1", "2", "3"]
func Max ¶
func Max[T constraints.Ordered](in []T) optional.O[T]
Max returns the maximum value in the slice.
EXAMPLE:
gslice.Max([]int{1, 2, 3}) 👉 3
gslice.Max([]int{}) 👉 0
func MaxBy ¶
func MaxBy[T constraints.Ordered](in []T, f func(T, T) bool) optional.O[T]
MaxBy returns the maximum value evaluated by f in the slice.
EXAMPLE:
gslice.MaxBy([]int{1, 2, 3} /*less = */, func(a, b int) bool { return a > b }).Must() 👉 1
func MaxN ¶
func MaxN[T constraints.Ordered](in ...T) optional.O[T]
MaxN returns the maximum value in the slice.
EXAMPLE:
gslice.MaxN(1, 2, 3) 👉 3
func Min ¶
func Min[T constraints.Ordered](in []T) optional.O[T]
Min returns the minimum value in the slice.
EXAMPLE:
gslice.Min([]int{1, 2, 3}) 👉 1
gslice.Min([]int{}) 👉 0
func MinBy ¶
func MinBy[T constraints.Ordered](in []T, f func(T, T) bool) optional.O[T]
MinBy returns the minimum value evaluated by f in the slice.
EXAMPLE:
gslice.MinBy([]int{3, 2, 1} /*less = */, func(a, b int) bool { return a > b }).Must() 👉 3
func MinN ¶
func MinN[T constraints.Ordered](in ...T) optional.O[T]
MinN returns the minimum value in the slice.
EXAMPLE:
gslice.MinN(1, 2, 3) 👉 1
func Repeat ¶ added in v0.1.5
Repeat returns a new slice with the elements repeated 'count' times.
EXAMPLE:
gslice.Repeat([]int{1, 2, 3}, 3) 👉 [1, 2, 3, 1, 2, 3, 1, 2, 3]
gslice.Repeat([]int{1, 2, 3}, 0) 👉 []int{}
func RepeatBy ¶ added in v0.1.5
RepeatBy returns a new slice with the elements return by f repeated 'count' times.
EXAMPLE:
gslice.RepeatBy(3, func(i int) int { return i }) 👉 [0, 1, 2]
gslice.RepeatBy(3, func(i int) string { return strconv.Itoa(i) }) 👉 []string{"1", "2", "3"}
func Replace ¶ added in v0.1.4
func Replace[T comparable, Slice ~[]T](in Slice, from, to T, count int) []T
Replace replaces the count elements in the slice from 'from' to 'to'.
EXAMPLE:
gslice.Replace([]int{1, 2, 3}, 2, 4, 1) 👉 [1, 4, 3]
gslice.Replace([]int{1, 2, 2}, 2, 4, -1) 👉 [1, 4, 4]
func ReplaceAll ¶ added in v0.1.4
func ReplaceAll[T comparable, Slice ~[]T](in Slice, from, to T) []T
ReplaceAll replaces all elements in the slice from 'from' to 'to'.
EXAMPLE:
gslice.ReplaceAll([]int{1, 2, 3}, 2, 4) 👉 [1, 4, 3]
gslice.ReplaceAll([]int{1, 2, 2}, 2, 4) 👉 [1, 4, 4]
func Reverse ¶ added in v0.1.4
func Reverse[T any, Slice ~[]T](in Slice)
Reverse reverses the slice.
EXAMPLE:
gslice.Reverse([]int{1, 2, 3}) 👉 [3, 2, 1]
gslice.Reverse([]int{}) 👉 []int{}
func ReverseClone ¶ added in v0.1.4
func ReverseClone[T any, Slice ~[]T](in Slice) Slice
ReverseClone reverses the slice.
EXAMPLE:
gslice.ReverseClone([]int{1, 2, 3}) 👉 [3, 2, 1]
gslice.ReverseClone([]int{}) 👉 []int{}
gslice.ReverseClone([]int{3, 2, 1}) 👉 [1, 2, 3]
func Shuffle ¶ added in v0.1.5
func Shuffle[T any, Slice ~[]T](in Slice) Slice
Shuffle shuffles the slice.
EXAMPLE:
gslice.Shuffle([]int{1, 2, 3}) 👉 [2, 1, 3] (random)
gslice.Shuffle([]int{}) 👉 []int{}
func ShuffleInPlace ¶ added in v0.1.5
func ShuffleInPlace[T any, Slice ~[]T](in Slice)
ShuffleInPlace shuffles the slice.
EXAMPLE:
array := []int{1, 2, 3}
gslice.ShuffleInPlace(array) 👉 [2, 1, 3] (random)
func Subset ¶ added in v0.1.4
Subset returns a subset slice from the slice. if start < -1 means that we take subset from right-to-left
EXAMPLE:
gslice.Subset([]int{1, 2, 3}, 0, 2) 👉 [1, 2]
gslice.Subset([]int{1, 2, 3}, -1, 2) 👉 [2, 3]
func SubsetInPlace ¶ added in v0.1.4
SubsetInPlace returns a subset slice copied from the slice. if start < -1 means that we take subset from right-to-left EXAMPLE:
gslice.SubsetInPlace([]int{1, 2, 3}, 0, 2) 👉 [1, 2]
gslice.SubsetInPlace([]int{1, 2, 3}, -1, 2) 👉 [2, 3]
Types ¶
This section is empty.