xslice

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 5 Imported by: 0

README ΒΆ

xslice

import "github.com/dashjay/xiter/xslice"

Index

func All

func All[T any](in []T, f func(T) bool) bool

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:

xslice.All([]int{1, 2, 3}, func(x int) bool { return x > 0 }) πŸ‘‰ true
xslice.All([]int{-1, 1, 2, 3}, func(x int) bool { return x > 0 }) πŸ‘‰ false

func AllEqual

func AllEqual[T comparable](in []T) bool

AllEqual checks if all elements in the slice are equal. Empty and single-element slices are considered to have all equal elements.

EXAMPLE:

xslice.AllEqual([]int{1, 1, 1, 1}) πŸ‘‰ true
xslice.AllEqual([]int{1, 2, 1, 1}) πŸ‘‰ false

func Any

func Any[T any](in []T, f func(T) bool) bool

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:

xslice.Any([]int{0, 1, 2, 3}, func(x int) bool { return x == 0 }) πŸ‘‰ true
xslice.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:

xslice.Avg([]int{1, 2, 3}) πŸ‘‰ float(2)
xslice.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:

xslice.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:

xslice.AvgN(1, 2, 3) πŸ‘‰ float(2)
xslice.AvgN() πŸ‘‰ float(0)

func Chunk

func Chunk[T any, Slice ~[]T](in Slice, chunkSize int) []Slice

Chunk returns a new slice with the elements in the slice chunked into smaller slices of the specified size.

EXAMPLE:

xslice.Chunk([]int{1, 2, 3, 4, 5}, 2) πŸ‘‰ [[1, 2], [3, 4], [5]]
xslice.Chunk([]int{1, 2, 3, 4, 5}, 10) πŸ‘‰ [[1, 2, 3, 4, 5]]
xslice.Chunk([]int{1, 2, 3, 4, 5}, 0) πŸ‘‰ []int{}

func ChunkInPlace

func ChunkInPlace[T any, Slice ~[]T](in Slice, chunkSize int) []Slice

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:

xslice.Chunk([]int{1, 2, 3, 4, 5}, 2) πŸ‘‰ [[1, 2], [3, 4], [5]]
xslice.Chunk([]int{1, 2, 3, 4, 5}, 10) πŸ‘‰ [[1, 2, 3, 4, 5]]
xslice.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:

xslice.Clone([]int{1, 2, 3}) πŸ‘‰ [1, 2, 3]

func CloneBy

func CloneBy[T any, U any](in []T, f func(T) U) []U

CloneBy returns a copy of the slice with the results of applying the given function to every element in this slice.

EXAMPLE:

xslice.CloneBy([]int{1, 2, 3}, func(x int) int { return x * 2 }) πŸ‘‰ [2, 4, 6]
xslice.CloneBy([]int{1, 2, 3}, strconv.Itoa) πŸ‘‰ ["1", "2", "3"]

func Compact

func Compact[T comparable, Slice ~[]T](in Slice) Slice

Compact returns a new slice with the zero elements removed.

EXAMPLE:

xslice.Compact([]int{0, 1, 2, 3, 4}) πŸ‘‰ [1 2 3 4]

func Concat

func Concat[T any](vs ...[]T) []T

Concat concatenates the slices.

EXAMPLE:

xslice.Concat([]int{1, 2, 3}, []int{4, 5, 6}) πŸ‘‰ [1, 2, 3, 4, 5, 6]
xslice.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:

xslice.Contains([]int{1, 2, 3}, 1) πŸ‘‰ true
xslice.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:

xslice.ContainsAll([]string{"1", "2", "3"}, []string{"1", "2", "3"})  πŸ‘‰ true
xslice.ContainsAll([]string{"1", "2", "3"}, []string{"1", "99", "1000"}) πŸ‘‰ false
xslice.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:

xslice.ContainsAny([]string{"1", "2", "3"}, []string{"1", "99", "1000"}) πŸ‘‰ true
xslice.ContainsAny([]string{"1", "2", "3"}, []string{"-1"}) πŸ‘‰ false
xslice.ContainsAny([]string{"1", "2", "3"}, []string{}) πŸ‘‰ false

func ContainsBy

func ContainsBy[T any](in []T, f func(T) bool) bool

ContainsBy returns true if the slice contains the value v evaluated by f.

EXAMPLE:

xslice.ContainsBy([]string{"1", "2", "3"}, func(x string) bool {
	i, _ := strconv.Atoi(x)
	return i == 1
}) πŸ‘‰ true

xslice.ContainsBy([]string{"1", "2", "3"}, func(x string) bool {
	i, _ := strconv.Atoi(x)
	return i == -1
}) πŸ‘‰ false

func Count

func Count[T any](in []T) int

Count returns the number of items in the slice.

EXAMPLE:

xslice.Count([]int{1, 2, 3}) πŸ‘‰ 3
xslice.Count([]int{}) πŸ‘‰ 0

func CountBy

func CountBy[T any, K comparable](in []T, fn func(T) K) map[K]int

CountBy counts occurrences of each key in the slice, returning a map of keys to counts.

EXAMPLE:

xslice.CountBy([]int{1, 2, 3, 2, 1, 2}, func(x int) int { return x })
// πŸ‘‰ map[int]int{1: 2, 2: 3, 3: 1}

func Difference

func Difference[T comparable, Slice ~[]T](left, right Slice) (onlyLeft, onlyRight Slice)

Difference returns two slices: the first slice contains the elements that are in the left slice but not in the right slice, and the second slice contains the elements that are in the right slice but not in the left slice.

EXAMPLE:

left := []int{1, 2, 3, 4, 5}
right := []int{4, 5, 6, 7, 8}
onlyLeft, onlyRight := xslice.Difference(left, right)
fmt.Println(onlyLeft)  // [1 2 3]
fmt.Println(onlyRight) // [6 7 8]

func Filter

func Filter[T any, Slice ~[]T](in Slice, f func(T) bool) Slice

Filter returns a new slice with the elements that satisfy the given function f.

EXAMPLE:

xslice.Filter([]int{1, 2, 3, 2, 4}, func(x int) bool { return x%2 == 0 }) πŸ‘‰ [2 4]

func Find

func Find[T any](in []T, f func(T) bool) (val T, found bool)

Find returns the first item in the slice that satisfies the condition provided by f.

EXAMPLE:

xslice.Find([]int{1, 2, 3}, func(x int) bool { return x == 1 })  πŸ‘‰ 1, true
xslice.Find([]int{1, 2, 3}, func(x int) bool { return x == -1 }) πŸ‘‰ 0, false

func FindO

func FindO[T any](in []T, f func(T) bool) optional.O[T]

FindO returns the first item in the slice that satisfies the condition provided by f.

EXAMPLE:

xslice.FindO(_range(0, 10), func(x int) bool { return x == 1 }).Must() πŸ‘‰ 1
xslice.FindO(_range(0, 10), func(x int) bool { return x == -1 }).Ok() πŸ‘‰ false

func First

func First[T any, Slice ~[]T](in Slice) (T, bool)

First returns the first element in the slice. If the slice is empty, the zero value of T is returned. EXAMPLE:

xslice.First([]int{1, 2, 3}) πŸ‘‰ 1
xslice.First([]int{}) πŸ‘‰ 0

func FirstO

func FirstO[T any, Slice ~[]T](in Slice) optional.O[T]

FirstO returns the first element in the slice as an optional.O[T]. If the slice is empty, the zero value of T is returned. EXAMPLE:

xslice.FirstO([]int{1, 2, 3}) πŸ‘‰ 1
xslice.FirstO([]int{}) πŸ‘‰ 0

func FlatMap

func FlatMap[T any, U any](in []T, fn func(T) []U) []U

FlatMap maps each element to a slice and flattens the results into a single slice.

EXAMPLE:

xslice.FlatMap([]int{1, 2, 3}, func(x int) []int { return []int{x, x * 10} })
// πŸ‘‰ []int{1, 10, 2, 20, 3, 30}

func Flatten

func Flatten[T any](in [][]T) []T

Flatten returns a new slice with all nested slices flattened into a single slice.

EXAMPLE:

xslice.Flatten([][]int{{1, 2}, {3, 4}, {5}}) πŸ‘‰ [1, 2, 3, 4, 5]
xslice.Flatten([][]int{{1, 2}, {}, {3, 4}}) πŸ‘‰ [1, 2, 3, 4]
xslice.Flatten([][]int{}) πŸ‘‰ []int{}
xslice.Flatten([][]int{{}, {}, {}}) πŸ‘‰ []int{}

func ForEach

func ForEach[T any](in []T, f func(T) bool)

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

func ForEachIdx[T any](in []T, f func(idx int, v T) bool)

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 GroupBy

func GroupBy[T any, K comparable, Slice ~[]T](in Slice, f func(T) K) map[K]Slice

GroupBy returns a map of the slice elements grouped by the given function f.

EXAMPLE:

xslice.GroupBy([]int{1, 2, 3, 2, 4}, func(x int) int { return x % 2 }) πŸ‘‰ map[0:[2 4] 1:[1 3]]

func GroupByMap

func GroupByMap[T any, Slice ~[]T, K comparable, V any](in Slice, f func(T) (K, V)) map[K][]V

GroupByMap returns a map of the slice elements grouped by the given function f.

EXAMPLE:

xslice.GroupByMap([]int{1, 2, 3, 2, 4}, func(x int) (int, int) { return x % 2, x }) πŸ‘‰ map[0:[2 4] 1:[1 3]]

func Head

func Head[T any](in []T) (v T, hasOne bool)

Head returns the first item in the slice.

EXAMPLE:

optional.FromValue2(xslice.Head(_range(0, 10))).Must() πŸ‘‰ 0
optional.FromValue2(xslice.Head(_range(0, 0))).Ok() πŸ‘‰ false

func HeadO

func HeadO[T any](in []T) optional.O[T]

HeadO returns the first item in the slice.

EXAMPLE:

xslice.HeadO(_range(0, 10)).Must() πŸ‘‰ 0
xslice.HeadO(_range(0, 0)).Ok() πŸ‘‰ false

func Index

func Index[T comparable, Slice ~[]T](in Slice, v T) int

Index returns the index of the first element in the slice that is equal to v. If no such element is found, -1 is returned. EXAMPLE:

xslice.Index([]int{1, 2, 3, 4, 5}, 1) πŸ‘‰ 0
xslice.Index([]int{1, 2, 3, 4, 5}, 3) πŸ‘‰ 2
xslice.Index([]int{1, 2, 3, 4, 5}, 666) πŸ‘‰ -1

func Intersect

func Intersect[T comparable, Slice ~[]T](left, right Slice) Slice

Intersect returns a slice that contains the elements that are in both left and right slices.

EXAMPLE:

left := []int{1, 2, 3, 4, 5}
right := []int{4, 5, 6, 7, 8}
intersect := xslice.Intersect(left, right)
fmt.Println(intersect) // [4 5]

func IsSorted

func IsSorted[T constraints.Ordered](in []T) bool

IsSorted checks if the slice is sorted in ascending order. Empty and single-element slices are considered sorted.

EXAMPLE:

xslice.IsSorted([]int{1, 2, 3, 4}) πŸ‘‰ true
xslice.IsSorted([]int{1, 3, 2, 4}) πŸ‘‰ false

func Join

func Join[T ~string](in []T, sep T) T

Join joins the slice with sep.

EXAMPLE:

xslice.Join([]string{"1", "2", "3"}, ".") πŸ‘‰ "1.2.3"
xslice.Join([]string{}, ".") πŸ‘‰ ""

func KeyBy

func KeyBy[T any, K comparable](in []T, fn func(T) K) map[K]T

KeyBy creates a map from the slice using the key function. Later elements overwrite earlier ones for duplicate keys.

EXAMPLE:

xslice.KeyBy([]int{1, 2, 3}, func(x int) int { return x * 10 })
// πŸ‘‰ map[int]int{10: 1, 20: 2, 30: 3}

func Last

func Last[T any, Slice ~[]T](in Slice) (T, bool)

Last returns the last element in the slice. If the slice is empty, the zero value of T is returned. EXAMPLE:

xslice.Last([]int{1, 2, 3}) πŸ‘‰ 3
xslice.Last([]int{}) πŸ‘‰ 0

func LastO

func LastO[T any, Slice ~[]T](in Slice) optional.O[T]

LastO returns the last element in the slice as an optional.O[T]. If the slice is empty, the zero value of T is returned. EXAMPLE:

xslice.LastO([]int{1, 2, 3}) πŸ‘‰ 3
xslice.LastO([]int{}) πŸ‘‰ 0

func Map

func Map[T any, U any](in []T, f func(T) U) []U

Map returns a new slice with the results of applying the given function to every element in this slice.

EXAMPLE:

xslice.Map([]int{1, 2, 3}, func(x int) int { return x * 2 }) πŸ‘‰ [2, 4, 6]
xslice.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:

xslice.Max([]int{1, 2, 3}) πŸ‘‰ 3
xslice.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:

xslice.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:

xslice.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:

xslice.Min([]int{1, 2, 3}) πŸ‘‰ 1
xslice.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:

xslice.MinBy([]int{3, 2, 1} /*less = */, func(a, b int) bool { return a > b }).Must() πŸ‘‰ 3

func MinMax

func MinMax[T constraints.Ordered](in []T) (min T, max T, ok bool)

MinMax returns the minimum and maximum elements in the slice in a single pass.

EXAMPLE:

xslice.MinMax([]int{3, 1, 4, 1, 5, 9}) πŸ‘‰ (1, 9, true)
xslice.MinMax([]int{}) πŸ‘‰ (0, 0, false)

func MinN

func MinN[T constraints.Ordered](in ...T) optional.O[T]

MinN returns the minimum value in the slice.

EXAMPLE:

xslice.MinN(1, 2, 3) πŸ‘‰ 1

func Mode

func Mode[T comparable](in []T) optional.O[T]

Mode returns the most frequently occurring element in the slice. If the slice is empty, it returns an empty optional. If there are multiple modes (tie), the first one to reach the maximum count is returned.

EXAMPLE:

xslice.Mode([]int{1, 2, 3, 2, 1, 2}) πŸ‘‰ 2
xslice.Mode([]int{}) πŸ‘‰ optional.Empty[int]()

func Partition

func Partition[T any, Slice ~[]T](in Slice, fn func(T) bool) (yes, no Slice)

Partition splits a slice into two slices based on a predicate. The first return slice contains elements where fn returns true.

EXAMPLE:

yes, no := xslice.Partition([]int{1, 2, 3, 4, 5}, func(x int) bool { return x%2 == 0 })
// yes πŸ‘‰ []int{2, 4}, no πŸ‘‰ []int{1, 3, 5}

func RandomElement

func RandomElement[T any, Slice ~[]T](in Slice) optional.O[T]

RandomElement returns a random element from the slice as an optional.O[T]. If the slice is empty, it returns an optional.O[T] with Ok() == false.

EXAMPLE:

xslice.RandomElement([]int{1, 2, 3, 4, 5}) πŸ‘‰ 3 (random element)
xslice.RandomElement([]int{42}) πŸ‘‰ 42 (always returns the only element)
xslice.RandomElement([]int{}).Ok() πŸ‘‰ false

func Remove

func Remove[T comparable, Slice ~[]T](in Slice, wantToRemove ...T) Slice

Remove returns a slice that remove all elements in wantToRemove

EXAMPLE:

arr := []int{1, 2, 3, 4}
arr1 := xslice.Remove(arr, 1)
fmt.Println(arr1) // [2, 3, 4]

func Repeat

func Repeat[T any, Slice ~[]T](in Slice, count int) Slice

Repeat returns a new slice with the elements repeated 'count' times.

EXAMPLE:

xslice.Repeat([]int{1, 2, 3}, 3) πŸ‘‰ [1, 2, 3, 1, 2, 3, 1, 2, 3]
xslice.Repeat([]int{1, 2, 3}, 0) πŸ‘‰ []int{}

func RepeatBy

func RepeatBy[T any](n int, f func(i int) T) []T

RepeatBy returns a new slice with the elements return by f repeated 'count' times.

EXAMPLE:

xslice.RepeatBy(3, func(i int) int { return i }) πŸ‘‰ [0, 1, 2]
xslice.RepeatBy(3, func(i int) string { return strconv.Itoa(i) }) πŸ‘‰ []string{"1", "2", "3"}

func Replace

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:

xslice.Replace([]int{1, 2, 3}, 2, 4, 1) πŸ‘‰ [1, 4, 3]
xslice.Replace([]int{1, 2, 2}, 2, 4, -1) πŸ‘‰ [1, 4, 4]

func ReplaceAll

func ReplaceAll[T comparable, Slice ~[]T](in Slice, from, to T) []T

ReplaceAll replaces all elements in the slice from 'from' to 'to'.

EXAMPLE:

xslice.ReplaceAll([]int{1, 2, 3}, 2, 4) πŸ‘‰ [1, 4, 3]
xslice.ReplaceAll([]int{1, 2, 2}, 2, 4) πŸ‘‰ [1, 4, 4]

func Reverse

func Reverse[T any, Slice ~[]T](in Slice)

Reverse reverses the slice.

EXAMPLE:

xslice.Reverse([]int{1, 2, 3}) πŸ‘‰ [3, 2, 1]
xslice.Reverse([]int{}) πŸ‘‰ []int{}

func ReverseClone

func ReverseClone[T any, Slice ~[]T](in Slice) Slice

ReverseClone reverses the slice.

EXAMPLE:

xslice.ReverseClone([]int{1, 2, 3}) πŸ‘‰ [3, 2, 1]
xslice.ReverseClone([]int{}) πŸ‘‰ []int{}
xslice.ReverseClone([]int{3, 2, 1}) πŸ‘‰ [1, 2, 3]

func Sample

func Sample[T any, Slice ~[]T](in Slice, n int) Slice

Sample returns a new slice with n randomly selected elements from the input slice. If n is greater than the length of the slice, it returns all elements in random order. If n is less than or equal to 0, it returns an empty slice.

EXAMPLE:

xslice.Sample([]int{1, 2, 3, 4, 5}, 3) πŸ‘‰ [3, 1, 5] (random order, 3 elements)
xslice.Sample([]int{1, 2, 3}, 5) πŸ‘‰ [2, 1, 3] (random order, all elements)
xslice.Sample([]int{1, 2, 3}, 0) πŸ‘‰ []int{}
xslice.Sample([]int{}, 3) πŸ‘‰ []int{}

func Shuffle

func Shuffle[T any, Slice ~[]T](in Slice) Slice

Shuffle shuffles the slice.

EXAMPLE:

xslice.Shuffle([]int{1, 2, 3}) πŸ‘‰ [2, 1, 3] (random)
xslice.Shuffle([]int{}) πŸ‘‰ []int{}

func ShuffleInPlace

func ShuffleInPlace[T any, Slice ~[]T](in Slice)

ShuffleInPlace shuffles the slice.

EXAMPLE:

array := []int{1, 2, 3}
xslice.ShuffleInPlace(array) πŸ‘‰ [2, 1, 3] (random)

func Subset

func Subset[T any, Slice ~[]T](in Slice, start, count int) Slice

Subset returns a subset slice from the slice. if start < -1 means that we take subset from right-to-left

EXAMPLE:

xslice.Subset([]int{1, 2, 3}, 0, 2) πŸ‘‰ [1, 2]
xslice.Subset([]int{1, 2, 3}, -1, 2) πŸ‘‰ [2, 3]

func SubsetInPlace

func SubsetInPlace[T any, Slice ~[]T](in Slice, start int, count int) Slice

SubsetInPlace returns a subset slice copied from the slice. if start < -1 means that we take subset from right-to-left EXAMPLE:

xslice.SubsetInPlace([]int{1, 2, 3}, 0, 2) πŸ‘‰ [1, 2]
xslice.SubsetInPlace([]int{1, 2, 3}, -1, 2) πŸ‘‰ [2, 3]

func Sum

func Sum[T constraints.Number, Slice ~[]T](in Slice) T

Sum returns the sum of all elements in the slice.

EXAMPLE:

xslice.Sum([]int{1, 2, 3}) πŸ‘‰ 6
xslice.Sum([]int{}) πŸ‘‰ 0

func SumBy

func SumBy[T any, R constraints.Number, Slice ~[]T](in Slice, f func(T) R) R

SumBy returns the sum of all elements in the slice after applying the given function f to each element.

EXAMPLE:

xslice.SumBy([]string{"1", "2", "3"}, func(x string) int {
	i, _ := strconv.Atoi(x)
	return i
}) πŸ‘‰ 6
xslice.SumBy([]string{}, func(x string) int { return 0 }) πŸ‘‰ 0

func SumN

func SumN[T constraints.Number](in ...T) T

SumN returns the sum of all input arguments.

EXAMPLE:

xslice.SumN(1, 2, 3) πŸ‘‰ 6
xslice.SumN() πŸ‘‰ 0

func ToMap

func ToMap[T comparable, U any](in []T, f func(T) U) map[T]U

ToMap returns a map where keys are elements from the slice and values are the result of applying f to each element. If there are duplicate keys in the slice, only the last element with that key will be present in the map.

EXAMPLE:

xslice.ToMap([]string{"a", "b", "c"}, func(s string) int { return len(s) }) πŸ‘‰ map[a:1 b:1 c:1]
xslice.ToMap([]int{1, 2, 3}, func(i int) string { return fmt.Sprintf("num_%d", i) }) πŸ‘‰ map[1:num_1 2:num_2 3:num_3]
xslice.ToMap([]int{1, 2, 1, 3}, func(i int) string { return fmt.Sprintf("val_%d", i) }) πŸ‘‰ map[1:val_1 2:val_2 3:val_3] (note: key 1 has "val_1" from the last occurrence)
xslice.ToMap([]int{}, func(i int) string { return "" }) πŸ‘‰ map[int]string{}

func Union

func Union[T comparable, Slice ~[]T](left, right Slice) Slice

Union returns a slice that contains all elements in left and right slices.

EXAMPLE:

left := []int{1, 2, 3, 4}
right := []int{3, 4, 5, 6}
union := xslice.Union(left, right)
fmt.Println(union) // [1 2 3 4 5 6]

func Uniq

func Uniq[T comparable, Slice ~[]T](in Slice) Slice

Uniq returns a new slice with the duplicate elements removed.

EXAMPLE:

xslice.Uniq([]int{1, 2, 3, 2, 4}) πŸ‘‰ [1, 2, 3, 4]

Generated by gomarkdoc

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func All ΒΆ

func All[T any](in []T, f func(T) bool) bool

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:

xslice.All([]int{1, 2, 3}, func(x int) bool { return x > 0 }) πŸ‘‰ true
xslice.All([]int{-1, 1, 2, 3}, func(x int) bool { return x > 0 }) πŸ‘‰ false

func AllEqual ΒΆ

func AllEqual[T comparable](in []T) bool

AllEqual checks if all elements in the slice are equal. Empty and single-element slices are considered to have all equal elements.

EXAMPLE:

xslice.AllEqual([]int{1, 1, 1, 1}) πŸ‘‰ true
xslice.AllEqual([]int{1, 2, 1, 1}) πŸ‘‰ false

func Any ΒΆ

func Any[T any](in []T, f func(T) bool) bool

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:

xslice.Any([]int{0, 1, 2, 3}, func(x int) bool { return x == 0 }) πŸ‘‰ true
xslice.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:

xslice.Avg([]int{1, 2, 3}) πŸ‘‰ float(2)
xslice.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:

xslice.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:

xslice.AvgN(1, 2, 3) πŸ‘‰ float(2)
xslice.AvgN() πŸ‘‰ float(0)

func Chunk ΒΆ

func Chunk[T any, Slice ~[]T](in Slice, chunkSize int) []Slice

Chunk returns a new slice with the elements in the slice chunked into smaller slices of the specified size.

EXAMPLE:

xslice.Chunk([]int{1, 2, 3, 4, 5}, 2) πŸ‘‰ [[1, 2], [3, 4], [5]]
xslice.Chunk([]int{1, 2, 3, 4, 5}, 10) πŸ‘‰ [[1, 2, 3, 4, 5]]
xslice.Chunk([]int{1, 2, 3, 4, 5}, 0) πŸ‘‰ []int{}

func ChunkInPlace ΒΆ

func ChunkInPlace[T any, Slice ~[]T](in Slice, chunkSize int) []Slice

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:

xslice.Chunk([]int{1, 2, 3, 4, 5}, 2) πŸ‘‰ [[1, 2], [3, 4], [5]]
xslice.Chunk([]int{1, 2, 3, 4, 5}, 10) πŸ‘‰ [[1, 2, 3, 4, 5]]
xslice.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:

xslice.Clone([]int{1, 2, 3}) πŸ‘‰ [1, 2, 3]

func CloneBy ΒΆ

func CloneBy[T any, U any](in []T, f func(T) U) []U

CloneBy returns a copy of the slice with the results of applying the given function to every element in this slice.

EXAMPLE:

xslice.CloneBy([]int{1, 2, 3}, func(x int) int { return x * 2 }) πŸ‘‰ [2, 4, 6]
xslice.CloneBy([]int{1, 2, 3}, strconv.Itoa) πŸ‘‰ ["1", "2", "3"]

func Compact ΒΆ

func Compact[T comparable, Slice ~[]T](in Slice) Slice

Compact returns a new slice with the zero elements removed.

EXAMPLE:

xslice.Compact([]int{0, 1, 2, 3, 4}) πŸ‘‰ [1 2 3 4]

func Concat ΒΆ

func Concat[T any](vs ...[]T) []T

Concat concatenates the slices.

EXAMPLE:

xslice.Concat([]int{1, 2, 3}, []int{4, 5, 6}) πŸ‘‰ [1, 2, 3, 4, 5, 6]
xslice.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:

xslice.Contains([]int{1, 2, 3}, 1) πŸ‘‰ true
xslice.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:

xslice.ContainsAll([]string{"1", "2", "3"}, []string{"1", "2", "3"})  πŸ‘‰ true
xslice.ContainsAll([]string{"1", "2", "3"}, []string{"1", "99", "1000"}) πŸ‘‰ false
xslice.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:

xslice.ContainsAny([]string{"1", "2", "3"}, []string{"1", "99", "1000"}) πŸ‘‰ true
xslice.ContainsAny([]string{"1", "2", "3"}, []string{"-1"}) πŸ‘‰ false
xslice.ContainsAny([]string{"1", "2", "3"}, []string{}) πŸ‘‰ false

func ContainsBy ΒΆ

func ContainsBy[T any](in []T, f func(T) bool) bool

ContainsBy returns true if the slice contains the value v evaluated by f.

EXAMPLE:

xslice.ContainsBy([]string{"1", "2", "3"}, func(x string) bool {
	i, _ := strconv.Atoi(x)
	return i == 1
}) πŸ‘‰ true

xslice.ContainsBy([]string{"1", "2", "3"}, func(x string) bool {
	i, _ := strconv.Atoi(x)
	return i == -1
}) πŸ‘‰ false

func Count ΒΆ

func Count[T any](in []T) int

Count returns the number of items in the slice.

EXAMPLE:

xslice.Count([]int{1, 2, 3}) πŸ‘‰ 3
xslice.Count([]int{}) πŸ‘‰ 0

func CountBy ΒΆ

func CountBy[T any, K comparable](in []T, fn func(T) K) map[K]int

CountBy counts occurrences of each key in the slice, returning a map of keys to counts.

EXAMPLE:

xslice.CountBy([]int{1, 2, 3, 2, 1, 2}, func(x int) int { return x })
// πŸ‘‰ map[int]int{1: 2, 2: 3, 3: 1}

func Difference ΒΆ

func Difference[T comparable, Slice ~[]T](left, right Slice) (onlyLeft, onlyRight Slice)

Difference returns two slices: the first slice contains the elements that are in the left slice but not in the right slice, and the second slice contains the elements that are in the right slice but not in the left slice.

EXAMPLE:

left := []int{1, 2, 3, 4, 5}
right := []int{4, 5, 6, 7, 8}
onlyLeft, onlyRight := xslice.Difference(left, right)
fmt.Println(onlyLeft)  // [1 2 3]
fmt.Println(onlyRight) // [6 7 8]

func Filter ΒΆ

func Filter[T any, Slice ~[]T](in Slice, f func(T) bool) Slice

Filter returns a new slice with the elements that satisfy the given function f.

EXAMPLE:

xslice.Filter([]int{1, 2, 3, 2, 4}, func(x int) bool { return x%2 == 0 }) πŸ‘‰ [2 4]

func Find ΒΆ

func Find[T any](in []T, f func(T) bool) (val T, found bool)

Find returns the first item in the slice that satisfies the condition provided by f.

EXAMPLE:

xslice.Find([]int{1, 2, 3}, func(x int) bool { return x == 1 })  πŸ‘‰ 1, true
xslice.Find([]int{1, 2, 3}, func(x int) bool { return x == -1 }) πŸ‘‰ 0, false

func FindO ΒΆ

func FindO[T any](in []T, f func(T) bool) optional.O[T]

FindO returns the first item in the slice that satisfies the condition provided by f.

EXAMPLE:

xslice.FindO(_range(0, 10), func(x int) bool { return x == 1 }).Must() πŸ‘‰ 1
xslice.FindO(_range(0, 10), func(x int) bool { return x == -1 }).Ok() πŸ‘‰ false

func First ΒΆ

func First[T any, Slice ~[]T](in Slice) (T, bool)

First returns the first element in the slice. If the slice is empty, the zero value of T is returned. EXAMPLE:

xslice.First([]int{1, 2, 3}) πŸ‘‰ 1
xslice.First([]int{}) πŸ‘‰ 0

func FirstO ΒΆ

func FirstO[T any, Slice ~[]T](in Slice) optional.O[T]

FirstO returns the first element in the slice as an optional.O[T]. If the slice is empty, the zero value of T is returned. EXAMPLE:

xslice.FirstO([]int{1, 2, 3}) πŸ‘‰ 1
xslice.FirstO([]int{}) πŸ‘‰ 0

func FlatMap ΒΆ

func FlatMap[T any, U any](in []T, fn func(T) []U) []U

FlatMap maps each element to a slice and flattens the results into a single slice.

EXAMPLE:

xslice.FlatMap([]int{1, 2, 3}, func(x int) []int { return []int{x, x * 10} })
// πŸ‘‰ []int{1, 10, 2, 20, 3, 30}

func Flatten ΒΆ

func Flatten[T any](in [][]T) []T

Flatten returns a new slice with all nested slices flattened into a single slice.

EXAMPLE:

xslice.Flatten([][]int{{1, 2}, {3, 4}, {5}}) πŸ‘‰ [1, 2, 3, 4, 5]
xslice.Flatten([][]int{{1, 2}, {}, {3, 4}}) πŸ‘‰ [1, 2, 3, 4]
xslice.Flatten([][]int{}) πŸ‘‰ []int{}
xslice.Flatten([][]int{{}, {}, {}}) πŸ‘‰ []int{}

func ForEach ΒΆ

func ForEach[T any](in []T, f func(T) bool)

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 ΒΆ

func ForEachIdx[T any](in []T, f func(idx int, v T) bool)

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 GroupBy ΒΆ

func GroupBy[T any, K comparable, Slice ~[]T](in Slice, f func(T) K) map[K]Slice

GroupBy returns a map of the slice elements grouped by the given function f.

EXAMPLE:

xslice.GroupBy([]int{1, 2, 3, 2, 4}, func(x int) int { return x % 2 }) πŸ‘‰ map[0:[2 4] 1:[1 3]]

func GroupByMap ΒΆ

func GroupByMap[T any, Slice ~[]T, K comparable, V any](in Slice, f func(T) (K, V)) map[K][]V

GroupByMap returns a map of the slice elements grouped by the given function f.

EXAMPLE:

xslice.GroupByMap([]int{1, 2, 3, 2, 4}, func(x int) (int, int) { return x % 2, x }) πŸ‘‰ map[0:[2 4] 1:[1 3]]
func Head[T any](in []T) (v T, hasOne bool)

Head returns the first item in the slice.

EXAMPLE:

optional.FromValue2(xslice.Head(_range(0, 10))).Must() πŸ‘‰ 0
optional.FromValue2(xslice.Head(_range(0, 0))).Ok() πŸ‘‰ false

func HeadO ΒΆ

func HeadO[T any](in []T) optional.O[T]

HeadO returns the first item in the slice.

EXAMPLE:

xslice.HeadO(_range(0, 10)).Must() πŸ‘‰ 0
xslice.HeadO(_range(0, 0)).Ok() πŸ‘‰ false

func Index ΒΆ

func Index[T comparable, Slice ~[]T](in Slice, v T) int

Index returns the index of the first element in the slice that is equal to v. If no such element is found, -1 is returned. EXAMPLE:

xslice.Index([]int{1, 2, 3, 4, 5}, 1) πŸ‘‰ 0
xslice.Index([]int{1, 2, 3, 4, 5}, 3) πŸ‘‰ 2
xslice.Index([]int{1, 2, 3, 4, 5}, 666) πŸ‘‰ -1

func Intersect ΒΆ

func Intersect[T comparable, Slice ~[]T](left, right Slice) Slice

Intersect returns a slice that contains the elements that are in both left and right slices.

EXAMPLE:

left := []int{1, 2, 3, 4, 5}
right := []int{4, 5, 6, 7, 8}
intersect := xslice.Intersect(left, right)
fmt.Println(intersect) // [4 5]

func IsSorted ΒΆ

func IsSorted[T constraints.Ordered](in []T) bool

IsSorted checks if the slice is sorted in ascending order. Empty and single-element slices are considered sorted.

EXAMPLE:

xslice.IsSorted([]int{1, 2, 3, 4}) πŸ‘‰ true
xslice.IsSorted([]int{1, 3, 2, 4}) πŸ‘‰ false

func Join ΒΆ

func Join[T ~string](in []T, sep T) T

Join joins the slice with sep.

EXAMPLE:

xslice.Join([]string{"1", "2", "3"}, ".") πŸ‘‰ "1.2.3"
xslice.Join([]string{}, ".") πŸ‘‰ ""

func KeyBy ΒΆ

func KeyBy[T any, K comparable](in []T, fn func(T) K) map[K]T

KeyBy creates a map from the slice using the key function. Later elements overwrite earlier ones for duplicate keys.

EXAMPLE:

xslice.KeyBy([]int{1, 2, 3}, func(x int) int { return x * 10 })
// πŸ‘‰ map[int]int{10: 1, 20: 2, 30: 3}

func Last ΒΆ

func Last[T any, Slice ~[]T](in Slice) (T, bool)

Last returns the last element in the slice. If the slice is empty, the zero value of T is returned. EXAMPLE:

xslice.Last([]int{1, 2, 3}) πŸ‘‰ 3
xslice.Last([]int{}) πŸ‘‰ 0

func LastO ΒΆ

func LastO[T any, Slice ~[]T](in Slice) optional.O[T]

LastO returns the last element in the slice as an optional.O[T]. If the slice is empty, the zero value of T is returned. EXAMPLE:

xslice.LastO([]int{1, 2, 3}) πŸ‘‰ 3
xslice.LastO([]int{}) πŸ‘‰ 0

func Map ΒΆ

func Map[T any, U any](in []T, f func(T) U) []U

Map returns a new slice with the results of applying the given function to every element in this slice.

EXAMPLE:

xslice.Map([]int{1, 2, 3}, func(x int) int { return x * 2 }) πŸ‘‰ [2, 4, 6]
xslice.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:

xslice.Max([]int{1, 2, 3}) πŸ‘‰ 3
xslice.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:

xslice.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:

xslice.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:

xslice.Min([]int{1, 2, 3}) πŸ‘‰ 1
xslice.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:

xslice.MinBy([]int{3, 2, 1} /*less = */, func(a, b int) bool { return a > b }).Must() πŸ‘‰ 3

func MinMax ΒΆ

func MinMax[T constraints.Ordered](in []T) (min T, max T, ok bool)

MinMax returns the minimum and maximum elements in the slice in a single pass.

EXAMPLE:

xslice.MinMax([]int{3, 1, 4, 1, 5, 9}) πŸ‘‰ (1, 9, true)
xslice.MinMax([]int{}) πŸ‘‰ (0, 0, false)

func MinN ΒΆ

func MinN[T constraints.Ordered](in ...T) optional.O[T]

MinN returns the minimum value in the slice.

EXAMPLE:

xslice.MinN(1, 2, 3) πŸ‘‰ 1

func Mode ΒΆ

func Mode[T comparable](in []T) optional.O[T]

Mode returns the most frequently occurring element in the slice. If the slice is empty, it returns an empty optional. If there are multiple modes (tie), the first one to reach the maximum count is returned.

EXAMPLE:

xslice.Mode([]int{1, 2, 3, 2, 1, 2}) πŸ‘‰ 2
xslice.Mode([]int{}) πŸ‘‰ optional.Empty[int]()

func Partition ΒΆ

func Partition[T any, Slice ~[]T](in Slice, fn func(T) bool) (yes, no Slice)

Partition splits a slice into two slices based on a predicate. The first return slice contains elements where fn returns true.

EXAMPLE:

yes, no := xslice.Partition([]int{1, 2, 3, 4, 5}, func(x int) bool { return x%2 == 0 })
// yes πŸ‘‰ []int{2, 4}, no πŸ‘‰ []int{1, 3, 5}

func RandomElement ΒΆ

func RandomElement[T any, Slice ~[]T](in Slice) optional.O[T]

RandomElement returns a random element from the slice as an optional.O[T]. If the slice is empty, it returns an optional.O[T] with Ok() == false.

EXAMPLE:

xslice.RandomElement([]int{1, 2, 3, 4, 5}) πŸ‘‰ 3 (random element)
xslice.RandomElement([]int{42}) πŸ‘‰ 42 (always returns the only element)
xslice.RandomElement([]int{}).Ok() πŸ‘‰ false

func Remove ΒΆ

func Remove[T comparable, Slice ~[]T](in Slice, wantToRemove ...T) Slice

Remove returns a slice that remove all elements in wantToRemove

EXAMPLE:

arr := []int{1, 2, 3, 4}
arr1 := xslice.Remove(arr, 1)
fmt.Println(arr1) // [2, 3, 4]

func Repeat ΒΆ

func Repeat[T any, Slice ~[]T](in Slice, count int) Slice

Repeat returns a new slice with the elements repeated 'count' times.

EXAMPLE:

xslice.Repeat([]int{1, 2, 3}, 3) πŸ‘‰ [1, 2, 3, 1, 2, 3, 1, 2, 3]
xslice.Repeat([]int{1, 2, 3}, 0) πŸ‘‰ []int{}

func RepeatBy ΒΆ

func RepeatBy[T any](n int, f func(i int) T) []T

RepeatBy returns a new slice with the elements return by f repeated 'count' times.

EXAMPLE:

xslice.RepeatBy(3, func(i int) int { return i }) πŸ‘‰ [0, 1, 2]
xslice.RepeatBy(3, func(i int) string { return strconv.Itoa(i) }) πŸ‘‰ []string{"1", "2", "3"}

func Replace ΒΆ

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:

xslice.Replace([]int{1, 2, 3}, 2, 4, 1) πŸ‘‰ [1, 4, 3]
xslice.Replace([]int{1, 2, 2}, 2, 4, -1) πŸ‘‰ [1, 4, 4]

func ReplaceAll ΒΆ

func ReplaceAll[T comparable, Slice ~[]T](in Slice, from, to T) []T

ReplaceAll replaces all elements in the slice from 'from' to 'to'.

EXAMPLE:

xslice.ReplaceAll([]int{1, 2, 3}, 2, 4) πŸ‘‰ [1, 4, 3]
xslice.ReplaceAll([]int{1, 2, 2}, 2, 4) πŸ‘‰ [1, 4, 4]

func Reverse ΒΆ

func Reverse[T any, Slice ~[]T](in Slice)

Reverse reverses the slice.

EXAMPLE:

xslice.Reverse([]int{1, 2, 3}) πŸ‘‰ [3, 2, 1]
xslice.Reverse([]int{}) πŸ‘‰ []int{}

func ReverseClone ΒΆ

func ReverseClone[T any, Slice ~[]T](in Slice) Slice

ReverseClone reverses the slice.

EXAMPLE:

xslice.ReverseClone([]int{1, 2, 3}) πŸ‘‰ [3, 2, 1]
xslice.ReverseClone([]int{}) πŸ‘‰ []int{}
xslice.ReverseClone([]int{3, 2, 1}) πŸ‘‰ [1, 2, 3]

func Sample ΒΆ

func Sample[T any, Slice ~[]T](in Slice, n int) Slice

Sample returns a new slice with n randomly selected elements from the input slice. If n is greater than the length of the slice, it returns all elements in random order. If n is less than or equal to 0, it returns an empty slice.

EXAMPLE:

xslice.Sample([]int{1, 2, 3, 4, 5}, 3) πŸ‘‰ [3, 1, 5] (random order, 3 elements)
xslice.Sample([]int{1, 2, 3}, 5) πŸ‘‰ [2, 1, 3] (random order, all elements)
xslice.Sample([]int{1, 2, 3}, 0) πŸ‘‰ []int{}
xslice.Sample([]int{}, 3) πŸ‘‰ []int{}

func Shuffle ΒΆ

func Shuffle[T any, Slice ~[]T](in Slice) Slice

Shuffle shuffles the slice.

EXAMPLE:

xslice.Shuffle([]int{1, 2, 3}) πŸ‘‰ [2, 1, 3] (random)
xslice.Shuffle([]int{}) πŸ‘‰ []int{}

func ShuffleInPlace ΒΆ

func ShuffleInPlace[T any, Slice ~[]T](in Slice)

ShuffleInPlace shuffles the slice.

EXAMPLE:

array := []int{1, 2, 3}
xslice.ShuffleInPlace(array) πŸ‘‰ [2, 1, 3] (random)

func Subset ΒΆ

func Subset[T any, Slice ~[]T](in Slice, start, count int) Slice

Subset returns a subset slice from the slice. if start < -1 means that we take subset from right-to-left

EXAMPLE:

xslice.Subset([]int{1, 2, 3}, 0, 2) πŸ‘‰ [1, 2]
xslice.Subset([]int{1, 2, 3}, -1, 2) πŸ‘‰ [2, 3]

func SubsetInPlace ΒΆ

func SubsetInPlace[T any, Slice ~[]T](in Slice, start int, count int) Slice

SubsetInPlace returns a subset slice copied from the slice. if start < -1 means that we take subset from right-to-left EXAMPLE:

xslice.SubsetInPlace([]int{1, 2, 3}, 0, 2) πŸ‘‰ [1, 2]
xslice.SubsetInPlace([]int{1, 2, 3}, -1, 2) πŸ‘‰ [2, 3]

func Sum ΒΆ

func Sum[T constraints.Number, Slice ~[]T](in Slice) T

Sum returns the sum of all elements in the slice.

EXAMPLE:

xslice.Sum([]int{1, 2, 3}) πŸ‘‰ 6
xslice.Sum([]int{}) πŸ‘‰ 0

func SumBy ΒΆ

func SumBy[T any, R constraints.Number, Slice ~[]T](in Slice, f func(T) R) R

SumBy returns the sum of all elements in the slice after applying the given function f to each element.

EXAMPLE:

xslice.SumBy([]string{"1", "2", "3"}, func(x string) int {
	i, _ := strconv.Atoi(x)
	return i
}) πŸ‘‰ 6
xslice.SumBy([]string{}, func(x string) int { return 0 }) πŸ‘‰ 0

func SumN ΒΆ

func SumN[T constraints.Number](in ...T) T

SumN returns the sum of all input arguments.

EXAMPLE:

xslice.SumN(1, 2, 3) πŸ‘‰ 6
xslice.SumN() πŸ‘‰ 0

func ToMap ΒΆ

func ToMap[T comparable, U any](in []T, f func(T) U) map[T]U

ToMap returns a map where keys are elements from the slice and values are the result of applying f to each element. If there are duplicate keys in the slice, only the last element with that key will be present in the map.

EXAMPLE:

xslice.ToMap([]string{"a", "b", "c"}, func(s string) int { return len(s) }) πŸ‘‰ map[a:1 b:1 c:1]
xslice.ToMap([]int{1, 2, 3}, func(i int) string { return fmt.Sprintf("num_%d", i) }) πŸ‘‰ map[1:num_1 2:num_2 3:num_3]
xslice.ToMap([]int{1, 2, 1, 3}, func(i int) string { return fmt.Sprintf("val_%d", i) }) πŸ‘‰ map[1:val_1 2:val_2 3:val_3] (note: key 1 has "val_1" from the last occurrence)
xslice.ToMap([]int{}, func(i int) string { return "" }) πŸ‘‰ map[int]string{}

func Union ΒΆ

func Union[T comparable, Slice ~[]T](left, right Slice) Slice

Union returns a slice that contains all elements in left and right slices.

EXAMPLE:

left := []int{1, 2, 3, 4}
right := []int{3, 4, 5, 6}
union := xslice.Union(left, right)
fmt.Println(union) // [1 2 3 4 5 6]

func Uniq ΒΆ

func Uniq[T comparable, Slice ~[]T](in Slice) Slice

Uniq returns a new slice with the duplicate elements removed.

EXAMPLE:

xslice.Uniq([]int{1, 2, 3, 2, 4}) πŸ‘‰ [1, 2, 3, 4]

Types ΒΆ

This section is empty.

Jump to

Keyboard shortcuts

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