Documentation
¶
Overview ¶
Package slices provides slice helpers.
Index ¶
- func ContainsAll[S ~[]E, E comparable](target E, lists ...S) bool
- func ContainsAny[S ~[]E, E comparable](target E, lists ...S) bool
- func ContainsFold[S ~[]E, E ~string](items S, target E) bool
- func Count[S ~[]E, E comparable](items S, target E) int
- func CountFunc[S ~[]E, E any](items S, match func(E) bool) int
- func Difference[S ~[]E, E comparable](items S, others ...S) S
- func Intersect[S ~[]E, E comparable](items S, others ...S) S
- func LastIndex[S ~[]E, E comparable](items S, target E) int
- func LastIndexFunc[S ~[]E, E any](items S, match func(E) bool) int
- func Partition[S ~[]E, E any](items S, match func(E) bool) (S, S)
- func SortNatural[S ~[]E, E ~string](s S)
- func Trim[S ~[]E, E comparable](items, cutset S) S
- func TrimLeft[S ~[]E, E comparable](items, cutset S) S
- func TrimRight[S ~[]E, E comparable](items, cutset S) S
- func Union[S ~[]E, E comparable](items S, others ...S) S
- func Unique[S ~[]E, E comparable](items S) S
- func UniqueFold[S ~[]E, E ~string](items S) S
- func UniqueFunc[S ~[]E, E any, K comparable](items S, key func(E) K) S
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContainsAll ¶
func ContainsAll[S ~[]E, E comparable](target E, lists ...S) bool
ContainsAll reports whether every one of the given `lists` contains `target`. It returns true when no `lists` are given.
Example ¶
Every list must contain the target; no lists reports true.
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.ContainsAll("a", []string{"a", "b"}, []string{"c", "a"}))
fmt.Println(xslices.ContainsAll("b", []string{"a", "b"}, []string{"c", "a"}))
fmt.Println(xslices.ContainsAll[[]string]("a"))
}
Output: true false true
func ContainsAny ¶
func ContainsAny[S ~[]E, E comparable](target E, lists ...S) bool
ContainsAny reports whether any of the given `lists` contains `target`.
Example ¶
A single list containing the target suffices; no lists reports false.
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.ContainsAny("b", []string{"a", "b"}, []string{"c", "d"}))
fmt.Println(xslices.ContainsAny("z", []string{"a", "b"}, []string{"c", "d"}))
fmt.Println(xslices.ContainsAny[[]string]("a"))
}
Output: true false false
func ContainsFold ¶
ContainsFold reports whether `items` contains `target` case-insensitively, using the same simple case-folding as strings.EqualFold.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
tags := []string{"Latest", "Stable"}
fmt.Println(xslices.ContainsFold(tags, "latest"))
fmt.Println(xslices.ContainsFold(tags, "STABLE"))
fmt.Println(xslices.ContainsFold(tags, "beta"))
}
Output: true true false
func Count ¶
func Count[S ~[]E, E comparable](items S, target E) int
Count returns the number of elements in `items` equal to `target`.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.Count([]string{"a", "b", "a", "c", "a"}, "a"))
fmt.Println(xslices.Count([]string{"a", "b"}, "z"))
}
Output: 3 0
func CountFunc ¶
CountFunc returns the number of elements in `items` satisfying `match`.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
isEven := func(n int) bool { return n%2 == 0 }
fmt.Println(xslices.CountFunc([]int{1, 2, 3, 4, 5, 6}, isEven))
}
Output: 3
func Difference ¶
func Difference[S ~[]E, E comparable](items S, others ...S) S
Difference returns the elements of `items` not present in any of `others`, preserving order and duplicates from `items`.
Example ¶
Duplicates and order in the first slice are preserved.
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.Difference([]int{1, 1, 2, 3}, []int{2}))
fmt.Println(xslices.Difference([]int{1, 2, 3}, []int{2}, []int{3}))
}
Output: [1 1 3] [1]
func Intersect ¶
func Intersect[S ~[]E, E comparable](items S, others ...S) S
Intersect returns the elements of `items` also present in every one of `others`, preserving order and duplicates from `items`.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.Intersect([]string{"a", "b", "c"}, []string{"c", "b", "d"}))
fmt.Println(xslices.Intersect([]int{1, 2, 3}, []int{2, 3}, []int{2}))
}
Output: [b c] [2]
func LastIndex ¶
func LastIndex[S ~[]E, E comparable](items S, target E) int
LastIndex returns the index of the last occurrence of `target` in `items`, or -1 if not present.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.LastIndex([]string{"a", "b", "a", "c"}, "a"))
fmt.Println(xslices.LastIndex([]string{"a", "b"}, "z"))
}
Output: 2 -1
func LastIndexFunc ¶
LastIndexFunc returns the index of the last element of `items` satisfying `match`, or -1 if none do.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
isEven := func(n int) bool { return n%2 == 0 }
fmt.Println(xslices.LastIndexFunc([]int{2, 3, 4, 5}, isEven))
fmt.Println(xslices.LastIndexFunc([]int{1, 3, 5}, isEven))
}
Output: 2 -1
func Partition ¶
Partition splits `items` into two slices: elements satisfying `match`, and elements that do not, preserving the original relative order in both.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
isEven := func(n int) bool { return n%2 == 0 }
even, odd := xslices.Partition([]int{1, 2, 3, 4, 5, 6}, isEven)
fmt.Println(even)
fmt.Println(odd)
}
Output: [2 4 6] [1 3 5]
func SortNatural ¶
func SortNatural[S ~[]E, E ~string](s S)
SortNatural sorts a string slice in place in natural order, so embedded numbers compare by value ("item2" before "item10") rather than lexically. See github.com/gechr/x/strings.CompareNatural.
Example ¶
Embedded numbers compare by value, so "item2" sorts before "item10".
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
items := []string{"item10", "item2", "item1", "item20", "item3"}
xslices.SortNatural(items)
fmt.Println(items)
}
Output: [item1 item2 item3 item10 item20]
func Trim ¶
func Trim[S ~[]E, E comparable](items, cutset S) S
Trim returns `items` with all leading and trailing elements contained in `cutset` removed. The result is a subslice of `items`, sharing its backing array.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.Trim([]int{0, 0, 1, 2, 0}, []int{0}))
fmt.Println(xslices.Trim([]string{"a", "b", "c", "b", "a"}, []string{"a", "b"}))
}
Output: [1 2] [c]
func TrimLeft ¶
func TrimLeft[S ~[]E, E comparable](items, cutset S) S
TrimLeft returns `items` with all leading elements contained in `cutset` removed. The result is a subslice of `items`, sharing its backing array.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.TrimLeft([]int{0, 0, 1, 2, 0}, []int{0}))
}
Output: [1 2 0]
func TrimRight ¶
func TrimRight[S ~[]E, E comparable](items, cutset S) S
TrimRight returns `items` with all trailing elements contained in `cutset` removed. The result is a subslice of `items`, sharing its backing array.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.TrimRight([]int{0, 0, 1, 2, 0}, []int{0}))
}
Output: [0 0 1 2]
func Union ¶
func Union[S ~[]E, E comparable](items S, others ...S) S
Union returns the elements of `items` followed by the elements of `others`, in first-seen order with duplicates removed.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.Union([]int{1, 2}, []int{2, 3}, []int{3, 4, 1}))
}
Output: [1 2 3 4]
func Unique ¶
func Unique[S ~[]E, E comparable](items S) S
Unique returns `items` in first-seen order with duplicates removed.
Example ¶
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.Unique([]string{"a", "b", "a", "A", "c", "b"}))
}
Output: [a b A c]
func UniqueFold ¶
func UniqueFold[S ~[]E, E ~string](items S) S
UniqueFold returns strings in first-seen order with duplicates removed case-insensitively, using the same simple case-folding as strings.EqualFold.
Example ¶
The first-seen spelling wins; folding matches strings.EqualFold, so Greek final sigma "ς", medial "σ", and capital "Σ" are duplicates.
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
fmt.Println(xslices.UniqueFold([]string{"Go", "GO", "go", "Rust"}))
fmt.Println(xslices.UniqueFold([]string{"ς", "σ", "Σ"}))
}
Output: [Go Rust] [ς]
func UniqueFunc ¶
func UniqueFunc[S ~[]E, E any, K comparable](items S, key func(E) K) S
UniqueFunc returns `items` in first-seen order with duplicates removed, where two items are duplicates when `key` reports the same value for both.
Example ¶
The first-seen item wins for each key.
package main
import (
"fmt"
xslices "github.com/gechr/x/slices"
)
func main() {
type user struct {
name string
age int
}
users := []user{{"alice", 30}, {"bob", 25}, {"alice", 40}}
fmt.Println(xslices.UniqueFunc(users, func(u user) string { return u.name }))
}
Output: [{alice 30} {bob 25}]
Types ¶
This section is empty.