slices

package
v2.19.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[S ~[]E, E any](s S, items ...E) S

Append adds the given items to the end of a given slice and returns the result.

Append([]int{1, 2, 3}, 4, 5) // []int{1, 2, 3, 4, 5}

func Chunk

func Chunk[S ~[]E, E any](s S, size int) (result []S)

Chunk returns a new slice containing the items of a given slice chunked into smaller slices of a given size.

Chunk([]int{1, 2, 3, 4, 5}, 2) // [][]int{{1, 2}, {3, 4}, {5}}
Chunk([]string{"a", "b", "c", "d", "e"}, 2) // [][]string{{"a", "b"}, {"c", "d"}, {"e"}}

func Concat

func Concat[S ~[]E, E any](slices ...S) S

Concat returns a new slice containing the items of all given slices.

Concat([]int{1, 2}, []int{3, 4}, []int{5, 6}) // []int{1, 2, 3, 4, 5, 6}

func Contains

func Contains[S ~[]E, E comparable](s S, e E) bool

Contains returns true if a given slice contains the given item, false otherwise.

Contains([]int{1, 2, 3}, 2) // true
Contains([]string{"a", "b", "c"}, "d") // false

func Difference

func Difference[S ~[]E, E comparable](s1, s2 S) S

Difference returns a new slice containing the items of a given slice that are not present in the other given slice.

Difference([]int{1, 2, 3}, []int{2, 3, 4}) // []int{1}
Difference([]string{"a", "b", "c"}, []string{"b", "c", "d"}) // []string{"a"}

func Each

func Each[S ~[]E, E any](s S, fn func(E, int))

Each calls the given function for each item of a given slice.

Each([]int{1, 2, 3}, func(i int, _ int) { fmt.Println(i) }) // 1\n2\n3\n

func Fill

func Fill[S ~[]E, E any](s S, value E) S

Fill returns a given slice with the given value.

Fill([]int{1, 2, 3}, 0) // []int{0, 0, 0}
Fill(make([]int, 3), 1) // []int{1, 1, 1}

func Filter

func Filter[S ~[]E, R []E, E any](s S, fn func(E, int) bool) R

Filter returns a new slice containing the items of a given slice that satisfy the given predicate function.

Filter([]int{1, 2, 3}, func(i int, _ int) bool { return i > 1 }) // []int{2, 3}
Filter([]string{"a", "b", "c"}, func(s string, _ int) bool { return s != "b" }) // []string{"a", "c"}

func Find

func Find[S ~[]E, E any](s S, fn func(E, int) bool) (E, bool)

Find returns the first item of a given slice that satisfies the given predicate function, or a zero value and false, if no item satisfies the predicate function.

Find([]int{1, 2, 3}, func(i int, _ int) bool { return i > 1 }) // 2, true
Find([]int{1, 2, 3}, func(i int, _ int) bool { return i > 3 }) // 0, false

func FindLast

func FindLast[S ~[]E, E any](s S, fn func(E, int) bool) (E, bool)

FindLast returns the last item of a given slice that satisfies the given predicate function, or a zero value and false, if no item satisfies the predicate function.

FindLast([]int{1, 2, 3}, func(i int, _ int) bool { return i > 1 }) // 3, true
FindLast([]int{1, 2, 3}, func(i int, _ int) bool { return i > 3 }) // 0, false

func First

func First[S ~[]E, E any](s S) (E, bool)

First returns the first item of a given slice, or a zero value and false if the slice is empty.

First([]int{1, 2, 3}) // 1, true
First([]int{}) // 0, false

func GroupBy

func GroupBy[S ~[]E, E any, K comparable](s S, fn func(E, int) K) map[K]S

GroupBy returns a new map containing the items of a given slice grouped by the result of the given function.

GroupBy([]int{1, 2, 3, 4, 5}, func(i int, _ int) int { return i % 2 }) // map[int][]int{0: {2, 4}, 1: {1, 3, 5}}

func Index

func Index[S ~[]E, E any](s S, fn func(E, int) bool) (int, bool)

Index returns the index of the first item of a given slice that satisfies the given predicate function, or -1 and false if no item satisfies the predicate function.

Index([]int{1, 2, 3}, func(i int, _ int) bool { return i > 1 }) // 1, true
Index([]int{1, 2, 3}, func(i int, _ int) bool { return i > 3 }) // -1, false

func IndexOf

func IndexOf[S ~[]E, E comparable](s S, e E) int

IndexOf returns the index of the first occurrence of the given item in a given slice, or -1 if the item is not found.

IndexOf([]int{1, 2, 3}, 2) // 1
IndexOf([]string{"a", "b", "c"}, "d") // -1

func Intersect

func Intersect[S ~[]E, E comparable](s1, s2 S) S

Intersect returns a new slice containing the items of a given slice that are also present in the other given slice.

Intersect([]int{1, 2, 3}, []int{2, 3, 4}) // []int{2, 3}
Intersect([]string{"a", "b", "c"}, []string{"b", "c", "d"}) // []string{"b", "c"}

func IsEmpty

func IsEmpty[S ~[]E, E any](s S) bool

IsEmpty returns true if a given slice is empty, false otherwise.

IsEmpty([]int{}) // true
IsEmpty([]int{1, 2, 3}) // false

func IsNotEmpty

func IsNotEmpty[S ~[]E, E any](s S) bool

IsNotEmpty returns true if a given slice is not empty, false otherwise.

IsNotEmpty([]int{}) // false
IsNotEmpty([]int{1, 2, 3}) // true

func KeyMap added in v2.11.0

func KeyMap[S ~[]E, E, R any, K comparable](s S, fn func(E, int) (K, R)) map[K]R

KeyMap returns a new map containing the results of applying the given function to each item of a given slice.

KeyMap([]int{1, 2, 3}, func(i, _ int) (int, int) { return i, i * 2 }) // map[int]int{1: 2, 2: 4, 3: 6}
KeyMap([]string{"a", "b", "c"}, func(s string, _ int) (string, string) {
	return s, s + "!"
}) // map[string]string{"a": "a!", "b": "b!", "c": "c!"}

func Last

func Last[S ~[]E, E any](s S) (E, bool)

Last returns the last item of a given slice, or a zero value and false if the slice is empty.

Last([]int{1, 2, 3}) // 3, true
Last([]int{}) // 0, false

func LastIndex

func LastIndex[S ~[]E, E any](s S, fn func(E, int) bool) (int, bool)

LastIndex returns the index of the last item of a given slice that satisfies the given predicate function, or -1 and false if no item satisfies the predicate function.

LastIndex([]int{1, 2, 3}, func(i int, _ int) bool { return i > 1 }) // 2, true
LastIndex([]int{1, 2, 3}, func(i int, _ int) bool { return i > 3 }) // -1, false

func LastIndexOf

func LastIndexOf[S ~[]E, E comparable](s S, e E) int

LastIndexOf returns the index of the last occurrence of the given item in a given slice, or -1 if the item is not found.

LastIndexOf([]int{1, 2, 3, 2}, 2) // 3
LastIndexOf([]string{"a", "b", "c"}, "d") // -1

func Length

func Length[S ~[]E, E any](s S) int

Length returns the length of a given slice.

Length([]int{1, 2, 3}) // 3
Length([]string{"a", "b", "c"}) // 3

func Map

func Map[S ~[]E, E, R any](s S, fn func(E, int) R) []R

Map returns a new slice containing the results of applying the given function to each item of a given slice.

Map([]int{1, 2, 3}, func(i, _ int) int { return i * 2 }) // []int{2, 4, 6}
Map([]string{"a", "b", "c"}, func(s string, _ int) string { return s + "!" }) // []string{"a!", "b!", "c!"}

func Max

func Max[S ~[]E, E constraints.Ordered](s S) E

Max returns the maximum item of a given slice.

Max([]int{1, 2, 3}) // 3
Max([]string{"a", "b", "c"}) // "c"

func Min

func Min[S ~[]E, E constraints.Ordered](s S) E

Min returns the minimum item of a given slice.

Min([]int{1, 2, 3}) // 1
Min([]string{"a", "b", "c"}) // "a"

func Only

func Only[S ~[]E, E comparable](s S, items ...E) S

Only returns a new slice only containing the items of a given

Only([]int{1, 2, 3}, 2, 3) // []int{2, 3}
Only([]string{"a", "b", "c"}, "b", "c") // []string{"b", "c"}

func Partition

func Partition[S ~[]E, E any](s S, fn func(E, int) bool) (yes, no S)

Partition returns two new slices, the first containing the items of a given slice that satisfy the given predicate function, and the second containing the items that do not satisfy the predicate function.

Partition([]int{1, 2, 3}, func(i int, _ int) bool { return i > 1 }) // ([]int{2, 3}, []int{1})

func Prepend

func Prepend[S ~[]E, E any](s S, items ...E) S

Prepend adds the given items to the beginning of a given slice and returns the result.

Prepend([]int{1, 2, 3}, 4, 5) // []int{4, 5, 1, 2, 3}

func Random

func Random[S ~[]E, E any](s S) E

Random returns a random item of a given slice.

Random([]int{1, 2, 3}) // 2
Random([]string{"a", "b", "c"}) // "b"

func Reduce

func Reduce[S ~[]E, E, R any](s S, fn func(R, E, int) R, defaults ...R) R

Reduce applies the given function against an accumulator and each element in the slice to reduce it to a single value.

Reduce([]int{1, 2, 3}, func(acc, i, _ int) int { return acc + i }, 0) // 6
Reduce([]string{"a", "b", "c"}, func(acc, s string, _ int) string { return acc + s }, "") // "abc"
Reduce([]int{1, 2, 3}, func(acc, i, _ int) int { return acc + i }, 5) // 11

func Reverse

func Reverse[S ~[]E, E any](s S) S

Reverse returns a new slice containing the items of a given slice in reverse order.

Reverse([]int{1, 2, 3}) // []int{3, 2, 1}

func Shuffle

func Shuffle[S ~[]E, E any](s S) S

Shuffle returns a new slice containing the items of a given slice shuffled.

Shuffle([]int{1, 2, 3}) // []int{3, 1, 2}

func Sum

func Sum[S ~[]E, E constraints.Numeric](s S) E

Sum returns the sum of the items of a given slice.

Sum([]int{1, 2, 3}) // 6

func Unique

func Unique[S ~[]E, E comparable](s S) S

Unique returns a new slice containing the unique items of a given slice.

Unique([]int{1, 2, 2, 3, 3, 3}) // []int{1, 2, 3}
Unique([]string{"a", "b", "b", "c", "c", "c"}) // []string{"a", "b", "c"}

func UniqueBy

func UniqueBy[S ~[]E, E any, K comparable](s S, fn func(E, int) K) S

UniqueBy returns a new slice containing the unique items of a given slice based on the given function.

UniqueBy([]string{"apple", "apple2", "cherry"}, func(s string, _ int) string {
	return s[:1]
}) // []string{"apple", "cherry"}

func Without

func Without[S ~[]E, E comparable](s S, items ...E) S

Without returns a new slice without the items of a given slice.

Without([]int{1, 2, 3}, 2, 3) // []int{1}
Without([]string{"a", "b", "c"}, "b", "c") // []string{"a"}

Types

This section is empty.

Jump to

Keyboard shortcuts

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