slicejez

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package slicejez slice相关函数

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendIfNotDuplicate

func AppendIfNotDuplicate[T comparable](list []T, item T) []T

AppendIfNotDuplicate 添加元素到切片,如果元素已经存在,则不添加。

Example
list := []string{"1", "2", "3"}

fmt.Println(AppendIfNotDuplicate(list, "1"))
fmt.Println(AppendIfNotDuplicate(list, "4"))
Output:

[1 2 3]
[1 2 3 4]

func AppendMultipleIfNotDuplicate

func AppendMultipleIfNotDuplicate[T comparable](list []T, items ...T) []T

AppendMultipleIfNotDuplicate 添加多个元素到切片,如果元素已经存在,则不添加。

Example
list := []string{"1", "2", "3"}

fmt.Println(AppendMultipleIfNotDuplicate(list, "1"))
fmt.Println(AppendMultipleIfNotDuplicate(list, "3", "4"))
fmt.Println(AppendMultipleIfNotDuplicate(list, "4", "5"))
Output:

[1 2 3]
[1 2 3 4]
[1 2 3 4 5]

func Contain

func Contain[T comparable](list []T, target T) bool

Contain 效验切片是否包含目标元素。

Example
list := []string{"1", "2", "3"}

fmt.Println(Contain(list, "1"))
fmt.Println(Contain(list, "4"))
Output:

true
false

func ContainAll

func ContainAll[T comparable](list []T, targets ...T) bool

ContainAll 效验切片是否包含所有的目标元素。

Example
list := []string{"1", "2", "3"}

fmt.Println(ContainAll(list, "1", "2", "3"))
fmt.Println(ContainAll(list, "1", "2", "2"))
fmt.Println(ContainAll(list, "1", "2"))
fmt.Println(ContainAll(list, "1", "4"))
Output:

true
true
true
false

func Difference

func Difference[T comparable](list1, list2 []T) []T

Difference 差集,结果不去重。

Example
list1 := []int{0, 1, 1, 2, 2, 3, 3, 0}

list2 := []int{0, 1, 1, 2, 2, 2, 6, 6}

fmt.Println(Difference(list1, list2))
Output:

[3 3 6 6]

func DifferenceUnique

func DifferenceUnique[T comparable](list1, list2 []T) []T

DifferenceUnique 差集,结果去重。

Example
list1 := []int{0, 1, 1, 2, 2, 3, 3, 0}

list2 := []int{0, 1, 1, 2, 2, 2, 6, 6}

fmt.Println(DifferenceUnique(list1, list2))
Output:

[3 6]

func Drop

func Drop[T any](list []T, n int) []T

Drop 返回从开头删除n个元素的切片,如果 n 大于切片的长度,则返回空切片。

Example
s := []string{"1", "2", "3", "4"}

fmt.Println(Drop(s, 2))
fmt.Println(Drop(s, 1111))
Output:

[3 4]
[]

func DropLast

func DropLast[T any](list []T, n int) []T

DropLast 返回从末尾删除n个元素的切片,如果 n 大于切片的长度,则返回空切片。

Example
s := []string{"1", "2", "3", "4"}

fmt.Println(DropLast(s, 2))
fmt.Println(DropLast(s, 1111))
Output:

[1 2]
[]

func Equal

func Equal[T comparable](list1, list2 []T) bool

Equal 长度、顺序、值都相等时返回 true 。

Example
list1 := []int{1, 2, 3, 3, 3}
list2 := []int{1, 2, 3, 3, 3}
list3 := []int{3, 2, 3, 3, 1}
list4 := []int{1, 2}

fmt.Println(Equal(list1, list2))
fmt.Println(Equal(list1, list3))
fmt.Println(Equal(list1, list4))
Output:

true
false
false

func EqualElement

func EqualElement[T comparable](list1 []T, list2 []T) bool

EqualElement 长度、值相等时返回 true ,不考虑顺序。

Example
list1 := []int{1, 2, 3, 3, 3}
list2 := []int{1, 2, 3, 3, 3}
list3 := []int{3, 2, 3, 3, 1}
list4 := []int{1, 2}
list5 := []int{3, 2, 1}

fmt.Println(EqualElement(list1, list2))
fmt.Println(EqualElement(list1, list3))
fmt.Println(EqualElement(list1, list4))
fmt.Println(EqualElement(list1, list5))
Output:

true
true
false
false

func Filter

func Filter[T any](list []T, iteratee func(index int, item T) bool) []T

Filter 遍历切片并为每个元素调用 iteratee 函数,只返回调用结果为true的元素。

Example
list := []string{"a", "b", "c"}

list2 := Filter(list, func(index int, item string) bool {
	return index != 1
})
fmt.Println(list2)
Output:

[a c]

func FilterMap

func FilterMap[T, U any](list []T, iteratee func(index int, item T) (U, bool)) []U

FilterMap 遍历切片并为每个元素调用 iteratee 函数,如果调用结果为true,则返回调用后元素。

Example
list := []string{"1", "2", "3"}

list2 := FilterMap(list, func(index int, item string) (int, bool) {
	i, _ := strconv.Atoi(item)
	if i == 2 {
		return i, true
	}
	return 0, false
})

fmt.Println(list2)
Output:

[2]

func FindDuplicates

func FindDuplicates[T comparable](list []T) []T

FindDuplicates 返回切片中所有重复的元素,结果不去重。

Example
list := []int{1, 2, 3, 3, 5, 3, 5, 6}

fmt.Println(FindDuplicates(list))
Output:

[3 3 5]

func FindIndex

func FindIndex[T comparable](list []T, target T) int

FindIndex 返回第一个匹配的元素的索引,不存在则返回 -1 。

Example
list := []int{1, 2, 3, 3, 5, 5, 6}

fmt.Println(FindIndex(list, 3))
fmt.Println(FindIndex(list, 5))
fmt.Println(FindIndex(list, 2))
fmt.Println(FindIndex(list, 9))
Output:

2
4
1
-1

func FindIndexFilter

func FindIndexFilter[T any](list []T, iteratee func(index int, item T) bool) int

FindIndexFilter 返回调用 iteratee 函数返回 true 的第一个元素的索引,不存在则返回 -1 。

Example
list := []int{1, 2, 3, 5, 3, 5, 6}

n := 0

fmt.Println(FindIndexFilter(list, func(index int, item int) bool {
	if item == 3 {
		n++
		if n > 1 {
			return true
		}
	}
	return false
}))

fmt.Println(FindIndexFilter(list, func(index int, item int) bool {
	return item > 3
}))
Output:

4
3

func FindUniqueDuplicates

func FindUniqueDuplicates[T comparable](list []T) []T

FindUniqueDuplicates 返回切片中所有重复的元素,结果去重。

Example
list := []int{1, 2, 3, 3, 5, 3, 5, 6}

fmt.Println(FindUniqueDuplicates(list))
Output:

[3 5]

func Flatten

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

Flatten 将二维切片转换为一维切片。

Example
list := [][]int{
	{1, 2, 3},
	{6, 7, 8},
}
list1 := Flatten(list)

fmt.Println(list1)
Output:

[1 2 3 6 7 8]

func ForEach

func ForEach[T any](list []T, iteratee func(index int, item T))

ForEach 遍历切片并为每个元素调用 iteratee 函数。

Example
list := []string{"a", "b", "c"}

items := make([]string, 0, len(list))
indexes := make([]int, 0, len(list))

ForEach(list, func(i int, item string) {
	items = append(items, item)
	indexes = append(indexes, i)
})

fmt.Println(items)
fmt.Println(indexes)
Output:

[a b c]
[0 1 2]

func ForEachWithBreak

func ForEachWithBreak[T any](list []T, iteratee func(index int, item T) bool)

ForEachWithBreak 遍历切片并为每个元素调用 iteratee 函数,如果返回 false,则停止遍历。

Example
list := []string{"a", "b", "c"}

items := make([]string, 0, len(list))
indexes := make([]int, 0, len(list))

ForEachWithBreak(list, func(index int, item string) bool {
	if index >= 2 {
		return false
	}

	items = append(items, item)
	indexes = append(indexes, index)
	return true
})

fmt.Println(items)
fmt.Println(indexes)
Output:

[a b]
[0 1]

func InsertAt

func InsertAt[T any](slice []T, index int, value ...T) []T

InsertAt 在切片的指定索引处插入值,如果索引大于切片的长度或小于 0,则将值附加到切片的末尾。

Example
list := []int{1, 2, 3, 4, 5}

list1 := InsertAt(list, 2, 666, 777, 888)

fmt.Println(list1)
Output:

[1 2 666 777 888 3 4 5]

func Intersection

func Intersection[T comparable](list1, list2 []T) []T

Intersection 交集,结果元素唯一。

Example
list1 := []int{0, 1, 1, 2, 2, 3, 3, 0}

list2 := []int{0, 1, 1, 2, 2, 2, 6, 6}

fmt.Println(Intersection(list1, list2))

list3 := []int{0, 1, 2, 3}
list4 := []int{2, 3, 4, 5, 6}

fmt.Println(Intersection(list3, list4))
Output:

[0 1 2]
[2 3]

func IsSorted

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

IsSorted 判断切片是否已排序。

Example
fmt.Println(IsSorted([]int{0, 1, 2, 3}))
fmt.Println(IsSorted([]string{"a", "b", "c", "d"}))
fmt.Println(IsSorted([]int{0, 1, 4, 3}))
fmt.Println(IsSorted([]string{"a", "e", "d", "c"}))
Output:

true
true
false
false

func IsSortedBy

func IsSortedBy[T any, K constraints.Ordered](list []T, iteratee func(item T) K) bool

IsSortedBy 遍历切片并为每个元素调用 iteratee 函数,以确定它是否已排序。

Example
fmt.Println(IsSortedBy([]string{"a", "bb", "ccc"}, func(s string) int {
	return len(s)
}))

fmt.Println(IsSortedBy([]string{"1", "2", "3", "11"}, func(s string) int {
	ret, _ := strconv.Atoi(s)
	return ret
}))

fmt.Println(IsSortedBy([]string{"aa", "b", "ccc"}, func(s string) int {
	return len(s)
}))
Output:

true
true
false

func Map

func Map[T, U any](list []T, iteratee func(index int, item T) U) []U

Map 遍历切片并为每个元素调用 iteratee 函数,并返回调用后结果。

Example
list := []string{"1", "2", "3"}

list2 := Map(list, func(index int, item string) int {
	i, _ := strconv.Atoi(item)
	return i
})

fmt.Println(list2)
Output:

[1 2 3]

func Max

func Max[T constraints.Ordered](list []T) T

Max 返回最大值

Example
list := []string{"1", "2", "3"}
fmt.Println(Max(list))

list2 := []int{5, 2, 111, 3}
fmt.Println(Max(list2))
Output:

3
111

func Min

func Min[T constraints.Ordered](list []T) T

Min 返回最小值

Example
list := []string{"1", "2", "3"}
fmt.Println(Min(list))

list2 := []int{5, 2, 111, 3}
fmt.Println(Min(list2))
Output:

1
2

func MutualDifference

func MutualDifference[T comparable](list1, list2 []T) ([]T, []T)

MutualDifference 差异,结果不去重。

返回值:

  • list1 中存在, list2 中不存在。
  • list2 中存在, list1 中不存在。
Example
list1 := []int{0, 1, 1, 2, 2, 2, 3, 0}
list2 := []int{0, 1, 1, 2, 2, 2, 6, 6}
fmt.Println(MutualDifference(list1, list2))

list3 := []int{0, 1, 2, 3}
list4 := []int{1, 2, 7, 8}
fmt.Println(MutualDifference(list3, list4))

var list5 []int
list6 := []int{1, 2, 3}
fmt.Println(MutualDifference(list5, list6))

list7 := []int{1, 2, 3}
var list8 []int
fmt.Println(MutualDifference(list7, list8))
Output:

[3] [6 6]
[0 3] [7 8]
[] [1 2 3]
[1 2 3] []

func Nonzero

func Nonzero[T comparable](list []T) []T

Nonzero 删除零值。

Example
list := []int{0, 1, 1, 2, 2, 2, 3, 0}

fmt.Println(Nonzero(list))
Output:

[1 1 2 2 2 3]

func Remove

func Remove[T comparable](list []T, items ...T) []T

Remove 从切片中删除元素。

Example
list := []string{"1", "2", "3"}

fmt.Println(Remove(list, "1"))
fmt.Println(Remove(list, "1", "2"))
Output:

[2 3]
[3]

func RemoveFilter

func RemoveFilter[T comparable](list []T, iteratee func(index int, item T) bool) []T

RemoveFilter 遍历切片并为每个元素调用 iteratee 函数,如果调用结果为true,则删除该元素。

Example
list := []string{"1", "2", "3"}

fmt.Println(RemoveFilter(list, func(index int, item string) bool {
	if item == "1" || item == "2" {
		return true
	}

	return false
}))
Output:

[3]

func Repeat

func Repeat[T any](item T, n int) []T

Repeat 返回包含 n 个 item 的切片。

Example
fmt.Println(Repeat(1, 5))
fmt.Println(Repeat("1", 5))
Output:

[1 1 1 1 1]
[1 1 1 1 1]

func Replace

func Replace[T comparable](list []T, old T, new T, n int) []T

Replace 将切片中的元素 old 替换为 new ,最多替换 n 次,如果 n 为-1,则替换所有的 old 元素。

Example
list := []int{0, 1, 1, 2, 2, 2, 3, 0}

fmt.Println(Replace(list, 1, 6, 1))
fmt.Println(Replace(list, 2, 6, -1))
Output:

[0 6 1 2 2 2 3 0]
[0 1 1 6 6 6 3 0]

func ReplaceAll

func ReplaceAll[T comparable](list []T, old T, new T) []T

ReplaceAll 将切片中的元素 old 替换为 new ,替换所有的 old 元素。

Example
list := []int{0, 1, 1, 2, 2, 2, 3, 0}

fmt.Println(ReplaceAll(list, 2, 6))
Output:

[0 1 1 6 6 6 3 0]

func Reverse

func Reverse[T any](slice []T)

Reverse 将切片中的元素顺序反转。

Example
list := []int{1, 2, 3, 4, 5}
Reverse(list)

fmt.Println(list)
Output:

[5 4 3 2 1]

func Slice

func Slice[T any](list []T, n, m int) []T

Slice 返回索引从 n 到 m 的切片,但不包括 m,等同于 slice[n:m],即[min,max),但不会在溢出时panic。

Example
s := []string{"1", "2", "3", "4"}

fmt.Println(Slice(s, 1, 3))
fmt.Println(Slice(s, 1, 4))
fmt.Println(Slice(s, 5, 10))
fmt.Println(Slice(s, 10, 5))
fmt.Println(Slice(s, 1, 6))
Output:

[2 3]
[2 3 4]
[]
[]
[2 3 4]

func ToMapBy

func ToMapBy[T any, K comparable, V any](list []T, iteratee func(index int, item T) (K, V)) map[K]V

ToMapBy 遍历切片,将切片中的元素转换为map的key和value。

Example
list := []int{1, 2, 3, 4, 5, 5, 5}

m := ToMapBy(list, func(index int, item int) (string, string) {
	itemStr := strconv.Itoa(item)
	return itemStr + "_" + strconv.Itoa(index), itemStr
})

fmt.Println(m)
Output:

map[1_0:1 2_1:2 3_2:3 4_3:4 5_4:5 5_5:5 5_6:5]

func Unique

func Unique[T comparable](list []T) []T

Unique 去重。

Example
list := []string{"1", "2", "3"}

fmt.Println(Unique(list))
fmt.Println(Unique([]string{"1", "2", "3", "3", "2"}))
Output:

[1 2 3]
[1 2 3]

func UniqueBy

func UniqueBy[T, U comparable](list []T, iteratee func(index int, item T) U) []U

UniqueBy 遍历切片并为每个元素调用 iteratee 函数,返回唯一的元素。

Example
list := []int{0, 1, 2, 3, 4, 5, 6}

fmt.Println(UniqueBy(list, func(index int, item int) string {
	if item > 3 {
		return "3"
	}
	return strconv.Itoa(item)
}))
Output:

[0 1 2 3]

func UniqueNonzero

func UniqueNonzero[T comparable](list []T) []T

UniqueNonzero 删除重复元素及零值。

Example
list := []int{0, 1, 0, 1, 2, 2, 2, 3}

fmt.Println(UniqueNonzero(list))
Output:

[1 2 3]

func UniqueNonzeroBy

func UniqueNonzeroBy[T, U comparable](list []T, iteratee func(index int, item T) U) []U

UniqueNonzeroBy 遍历切片并为每个元素调用 iteratee 函数,返回唯一的、非零值的元素。

Example
list := []int{0, 1, 0, 1, 2, 2, 2, 3}

fmt.Println(UniqueNonzeroBy(list, func(index int, item int) string {
	return strconv.Itoa(item)
}))
Output:

[1 2 3]

Types

type SafeSlice added in v1.0.5

type SafeSlice[T comparable] struct {
	// contains filtered or unexported fields
}

SafeSlice 并发安全的切片。

func NewSafeSlice added in v1.0.5

func NewSafeSlice[T comparable](list []T) *SafeSlice[T]

NewSafeSlice 创建一个并发安全的切片。

func (*SafeSlice[T]) Append added in v1.0.5

func (s *SafeSlice[T]) Append(items ...T)

Append 添加元素到切片。

func (*SafeSlice[T]) AppendIfNotDuplicate added in v1.0.5

func (s *SafeSlice[T]) AppendIfNotDuplicate(item T)

AppendIfNotDuplicate 添加元素到切片,如果元素已经存在,则不添加。

func (*SafeSlice[T]) AppendMultipleIfNotDuplicate added in v1.0.5

func (s *SafeSlice[T]) AppendMultipleIfNotDuplicate(items ...T)

AppendMultipleIfNotDuplicate 添加多个元素到切片,如果元素已经存在,则不添加。

func (*SafeSlice[T]) Filter added in v1.0.5

func (s *SafeSlice[T]) Filter(iteratee func(index int, item T) bool) []T

Filter 遍历切片并为每个元素调用 iteratee 函数,只返回调用结果为true的元素。

func (*SafeSlice[T]) ForEach added in v1.0.5

func (s *SafeSlice[T]) ForEach(iteratee func(index int, item T))

ForEach 遍历切片并为每个元素调用 iteratee 函数。

func (*SafeSlice[T]) ForEachWithBreak added in v1.0.5

func (s *SafeSlice[T]) ForEachWithBreak(iteratee func(index int, item T) bool)

ForEachWithBreak 遍历切片并为每个元素调用 iteratee 函数,如果返回 false,则停止遍历。

func (*SafeSlice[T]) Index added in v1.0.5

func (s *SafeSlice[T]) Index(item T) int

Index 返回指定元素在切片中的索引位置。

func (*SafeSlice[T]) Insert added in v1.0.5

func (s *SafeSlice[T]) Insert(index int, items ...T)

Insert 在指定索引位置插入元素。

func (*SafeSlice[T]) Len added in v1.0.5

func (s *SafeSlice[T]) Len() int

Len 返回切片的长度。

func (*SafeSlice[T]) Load added in v1.0.5

func (s *SafeSlice[T]) Load() []T

Load 返回切片的副本。

func (*SafeSlice[T]) LoadByIndex added in v1.0.5

func (s *SafeSlice[T]) LoadByIndex(index int) T

LoadByIndex 返回指定索引位置的元素,-1 则返回最后一个元素,如果索引超出范围,panic。

func (*SafeSlice[T]) Remove added in v1.0.5

func (s *SafeSlice[T]) Remove(items ...T)

Remove 从切片中移除元素。

func (*SafeSlice[T]) RemoveByIndex added in v1.0.5

func (s *SafeSlice[T]) RemoveByIndex(index int)

RemoveByIndex 从切片中移除指定索引位置的元素。

func (*SafeSlice[T]) Replace added in v1.0.5

func (s *SafeSlice[T]) Replace(old T, new T, n int)

Replace 将切片中的元素 old 替换为 new ,最多替换 n 次,如果 n 为-1,则替换所有的 old 元素。

func (*SafeSlice[T]) ReplaceByIndex added in v1.0.5

func (s *SafeSlice[T]) ReplaceByIndex(index int, new T)

ReplaceByIndex 将指定索引位置的元素替换为 new 。

func (*SafeSlice[T]) Slice added in v1.0.5

func (s *SafeSlice[T]) Slice(n, m int) []T

Slice 返回索引从 n 到 m 的切片,但不包括 m,等同于 slice[n:m],即[min,max),但不会在溢出时panic。

Jump to

Keyboard shortcuts

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