Documentation
¶
Index ¶
- func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice
- func FilterI[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice
- func Map[T any, Slice ~[]T](collection Slice, fn func(item T) T)
- func MapI[T any, Slice ~[]T](collection Slice, fn func(item T, index int) T)
- func Reverse[T any, Slice ~[]T](collection Slice)
- func Shuffle[T any, Slice ~[]T](collection Slice)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶ added in v1.50.0
Filter is a generic function that modifies the input slice in-place to contain only the elements that satisfy the provided predicate function. The predicate function takes an element of the slice and its index, and should return true for elements that should be kept and false for elements that should be removed. The function returns the modified slice, which may be shorter than the original if some elements were removed. Note that the order of elements in the original slice is preserved in the output. Play: https://go.dev/play/p/0jY3Z0B7O_5
Example ¶
list := []int{1, 2, 3, 4}
newList := Filter(list, func(nbr int) bool {
return nbr%2 == 0
})
fmt.Printf("%v\n%v", list, newList)
Output: [2 4 3 4] [2 4]
func FilterI ¶ added in v1.50.0
FilterI is a generic function that modifies the input slice in-place to contain only the elements that satisfy the provided predicate function. The predicate function takes an element of the slice and its index, and should return true for elements that should be kept and false for elements that should be removed. The function returns the modified slice, which may be shorter than the original if some elements were removed. Note that the order of elements in the original slice is preserved in the output.
Example ¶
list := []int{1, 2, 3, 4}
newList := Filter(list, func(nbr int) bool {
return nbr%2 == 0
})
fmt.Printf("%v\n%v", list, newList)
Output: [2 4 3 4] [2 4]
func Map ¶ added in v1.50.0
func Map[T any, Slice ~[]T](collection Slice, fn func(item T) T)
Map is a generic function that modifies the input slice in-place to contain the result of applying the provided function to each element of the slice. The function returns the modified slice, which has the same length as the original. Play: https://go.dev/play/p/0jY3Z0B7O_5
Example ¶
list := []int{1, 2, 3, 4}
Map(list, func(nbr int) int {
return nbr * 2
})
fmt.Printf("%v", list)
Output: [2 4 6 8]
func MapI ¶ added in v1.50.0
MapI is a generic function that modifies the input slice in-place to contain the result of applying the provided function to each element of the slice. The function returns the modified slice, which has the same length as the original.
Example ¶
list := []int{1, 2, 3, 4}
MapI(list, func(nbr, index int) int {
return nbr * index
})
fmt.Printf("%v", list)
Output: [0 2 6 12]
func Reverse ¶
func Reverse[T any, Slice ~[]T](collection Slice)
Reverse reverses a slice so that the first element becomes the last, the second element becomes the second to last, and so on. Play: https://go.dev/play/p/O-M5pmCRgzV
Example ¶
list := []int{0, 1, 2, 3, 4, 5}
Reverse(list)
fmt.Printf("%v", list)
Output: [5 4 3 2 1 0]
func Shuffle ¶
func Shuffle[T any, Slice ~[]T](collection Slice)
Shuffle returns a slice of shuffled values. Uses the Fisher-Yates shuffle algorithm. Play: https://go.dev/play/p/2xb3WdLjeSJ
Example ¶
list := []int{0, 1, 2, 3, 4, 5}
Shuffle(list)
fmt.Printf("%v", list)
Types ¶
This section is empty.