slicex

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package slicex provides some extra slice generic functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert[S1 ~[]E1, E1, E2 any](vs S1, convert func(E1) E2) []E2

Convert converts the slice from []E1 to []E2.

Example
type Ints []int

ints1 := []int{1, 2, 3}
ints2 := Ints{4, 5, 6}
int64s1 := Convert(ints1, func(v int) int64 { return int64(v) })
int64s2 := Convert(ints2, func(v int) int64 { return int64(v) })

fmt.Println(int64s1)
fmt.Println(int64s2)
Output:

[1 2 3]
[4 5 6]

func Interfaces

func Interfaces[S ~[]E, E any](vs S) []interface{}

Interfaces converts []any to []interface{}.

Example
ss := []string{"a", "b", "c"}
vs1 := Interfaces(ss)
fmt.Printf("%T: %v\n", vs1, vs1)

ints := []int{1, 2, 3}
vs2 := Interfaces(ints)
fmt.Printf("%T: %v\n", vs2, vs2)
Output:

[]interface {}: [a b c]
[]interface {}: [1 2 3]

func Make

func Make[S ~[]E, E any, I ~int | ~int64](len, cap, defaultCap I) S

Make returns a new slice.

If both cap and defaultCap are equal to 0, it is equal to make(S, len). If cap is equal to 0, use defaultCap as cap instead, which is equal to make(S, len, defaultCap).

func Merge

func Merge[S ~[]E, E any](ss ...S) S

Merge merges a set of slices in turn to one slice.

If no arguments, return nil. If all the arguments are empty, return a empty slice with cap==0.

func SetEqual

func SetEqual[S ~[]E, E comparable](vs1, vs2 S) bool

SetEqual reports whether the element set of the two slices are equal.

Example
s1 := []string{"a", "b", "c"}
s2 := []string{"b", "c", "a"}
s3 := []string{"a", "b", "b"}
if SetEqual(s1, s2) {
	fmt.Printf("%v is equal to %v\n", s1, s2)
} else {
	fmt.Printf("%v is not equal to %v\n", s1, s2)
}

if SetEqual(s1, s3) {
	fmt.Printf("%v is equal to %v\n", s1, s3)
} else {
	fmt.Printf("%v is not equal to %v\n", s1, s3)
}
Output:

[a b c] is equal to [b c a]
[a b c] is not equal to [a b b]

Types

This section is empty.

Jump to

Keyboard shortcuts

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