glist

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Associate added in v1.2.0

func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V

Associate 类似KeyBy,将切片转换为map,key为切片原数据,value为函数处理的结果.如果存在重复的,则忽略掉. Play: https://go.dev/play/p/WHa2CfMO3Lr

func Chunk added in v1.2.0

func Chunk[T any](collection []T, size int) [][]T

Chunk 返回一个元素数组,将其分成长度为size的小切片.如果切片不能被平均分割,最后一个块将是剩余的元素. @param: collection 切片 @param: size 每个切片分组里数据的大小

func Compact added in v1.2.0

func Compact[T comparable](collection []T) []T

Compact 清除切片空值 Play: https://go.dev/play/p/tXiy-iK6PAc

func Contains added in v1.2.0

func Contains[T comparable](collection []T, element T) bool

Contains 判断切片是否包含指定的元素 @param: collection 原切片数据 @param: element 查找的数据

func ContainsBy added in v1.2.0

func ContainsBy[T any](collection []T, predicate func(item T) bool) bool

ContainsBy 和FindIndex类似,判断切片是否包含指定的元素(根据指定的函数) @param: collection 原切片数据 @param: predicate 进行比较的函数

func Count added in v1.2.0

func Count[T comparable](collection []T, value T) (count int)

Count 统计元素出现的次数 Play: https://go.dev/play/p/Y3FlK54yveC

func CountBy added in v1.2.0

func CountBy[T any](collection []T, predicate func(item T) bool) (count int)

CountBy 统计满足条件元素出现的次数 Play: https://go.dev/play/p/ByQbNYQQi4X

func CountValues added in v1.2.0

func CountValues[T comparable](collection []T) map[T]int

CountValues 统计切片里元素出现的次数,返回的map,key为切片的元素,value为次数 Play: https://go.dev/play/p/-p-PyLT4dfy

func CountValuesBy added in v1.2.0

func CountValuesBy[T any, U comparable](collection []T, mapper func(item T) U) map[U]int

CountValuesBy 统计切片里元素满足条件的次数,返回的map,key为函数的结果,value为满足的元素个数 Play: https://go.dev/play/p/2U0dG1SnOmS

func Difference added in v1.2.0

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

Difference 和SliceDiff一样,计算2个集合的交集

func DistinctInt added in v1.1.4

func DistinctInt(slice []int) []int

DistinctInt 整型数组去重

func DistinctItem added in v1.1.4

func DistinctItem[T any](slice []T, compare func(T, T) bool) []T

DistinctItem 数组去重

func DistinctStr added in v1.1.4

func DistinctStr(slice []string) []string

DistinctStr 字符串数组去重

func Drop added in v1.2.0

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

Drop 从开头丢弃n个元素 Play: https://go.dev/play/p/JswS7vXRJP2

func DropRight added in v1.2.0

func DropRight[T any](collection []T, n int) []T

DropRight 从结尾丢弃n个元素. Play: https://go.dev/play/p/GG0nXkSJJa3

func DropRightWhile added in v1.2.0

func DropRightWhile[T any](collection []T, predicate func(item T) bool) []T

DropRightWhile DropWhile的尾部丢弃 Play: https://go.dev/play/p/3-n71oEC0Hz

func DropWhile added in v1.2.0

func DropWhile[T any](collection []T, predicate func(item T) bool) []T

DropWhile 类似于Filter,丢弃满足条件的索引前面的数据 Play: https://go.dev/play/p/7gBPYw2IK16

func Every added in v1.2.0

func Every[T comparable](collection []T, subset []T) bool

Every 判断一个切片是否完全包含另一个切片 @param: collection 大切片 @param: subset 小切片

func EveryBy added in v1.2.0

func EveryBy[T any](collection []T, predicate func(item T) bool) bool

EveryBy 判断一个切片的元素进行函数计算全部返回true @param: collection 大切片 @param: predicate 计算函数

func Fill added in v1.2.0

func Fill[T Cloneable[T]](collection []T, initial T) []T

Fill 用"初始值"填充数组元素,initial需要实现Cloneable接口 https://go.dev/play/p/VwR34GzqEub

func Filter added in v1.2.0

func Filter[T any](collection []T, predicate func(item T, index int) bool) []T

Filter 遍历集合的元素,返回满足条件的元素 @param: collection原切片 @param: predicate执行计算的函数,返回true才满足过滤的条件

func FilterMap added in v1.2.0

func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R

FilterMap 遍历切片并根据给定的函数计算的结果,将原切片数据计算结果为true,计算后的数据合并至新的切片 @param: collection原切片 @param: callback执行计算的函数,返true才将计算的结果合并至新的切片

func FindIndex

func FindIndex[T any](s []T, compare func(T) bool) (int, bool)

FindIndex 查询泛型在切片里的位置

func FindIntIndex

func FindIntIndex(slice []int, val int) (int, bool)

FindIntIndex 查询数字在切片里的位置

func FindLastIndexOf added in v1.2.0

func FindLastIndexOf[T any](collection []T, predicate func(item T) bool) (int, bool)

FindLastIndexOf 查询泛型在切片里最后一次的位置

func FindStrIndex

func FindStrIndex(slice []string, val string) (int, bool)

FindStrIndex 查询字符串在切片里的位置

func FlatMap added in v1.2.0

func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R

FlatMap 操作切片并将其转换和展平为另一种类型的切片 创建一个扁平化(注:同阶数组)的切片,这个切片的值来自collection(切片)中的每一个值经过iteratee(迭代函数)处理后返回的结果,并且扁平化合并. @param: collection原切片 @param: iteratee执行计算的函数,函数计算的结果是一个新的切片

func Flatten added in v1.2.0

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

Flatten 返回深度仅为一级的切片,即二维切片转换为一维切片

func ForEach added in v1.2.0

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

ForEach 遍历集合的元素并为每个元素调用iteratee函数

func GroupBy added in v1.2.0

func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T

GroupBy 返回由通过iteratee运行集合的每个元素的结果生成的键组成的对象 @param: collection切片 @param: accumulator执行计算的函数,根据函数的结果进行分组,函数的结果作为分组后map的key,value是计算结果相同的元素的切片

func Interleave added in v1.2.0

func Interleave[T any](collections ...[]T) []T

Interleave 循环交替输入切片,并依次在索引处追加值到结果中

func Intersect added in v1.2.0

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

Intersect 和SliceIntersect一样,计算2个切片的交集

func IsSorted added in v1.2.0

func IsSorted[T Ordered](collection []T) bool

IsSorted 检查切片是否是已排序的 Play: https://go.dev/play/p/mc3qR-t4mcx

func IsSortedByKey added in v1.2.0

func IsSortedByKey[T any, K Ordered](collection []T, iteratee func(item T) K) bool

IsSortedByKey 检查一个切片是否按iteratee函数的结果排序. Play: https://go.dev/play/p/wiG6XyBBu49

func KeyBy added in v1.2.0

func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V

KeyBy 将切片转换为map,key为函数处理的结果,value为切片元数据. Play: https://go.dev/play/p/mdaClUAT-zZ

func Last added in v1.2.0

func Last[T any](collection []T) (T, error)

Last returns the last element of a collection or error if empty.

func LastIndexOf added in v1.2.0

func LastIndexOf[T comparable](collection []T, element T) int

LastIndexOf 返回数组中最后出现的值所在的索引,没有找到的话返回-1

func Map added in v1.2.0

func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R

Map 遍历切片并根据给定的函数将原切片值进行转换后合并为一个新的切片 @param: collection原切片 @param: iteratee执行计算的函数,返回计算后的数据后合并至新的切片

func Max added in v1.2.0

func Max[T Ordered](collection []T) T

Max 返回切片的最大值

func MaxBy added in v1.2.0

func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T

MaxBy 根据给定的比较函数,返回切片的最大值

func Min added in v1.2.0

func Min[T Ordered](collection []T) T

Min 返回切片的最小值

func MinBy added in v1.2.0

func MinBy[T any](collection []T, comparison func(a T, b T) bool) T

MinBy 根据给定的比较函数,返回切片的最小值

func None added in v1.2.0

func None[T comparable](collection []T, subset []T) bool

None 如果切片中没有包含子集的元素(和子集的元素没有重叠),或者子集为空,则返回true

func NoneBy added in v1.2.0

func NoneBy[T any](collection []T, predicate func(item T) bool) bool

NoneBy 切片元素经过函数计算结果都是false

func Nth added in v1.2.0

func Nth[T any, N Integer](collection []T, nth N) (T, error)

Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds.

func ParallelForEach added in v1.2.0

func ParallelForEach[T any](collection []T, iteratee func(item T, index int))

ParallelForEach ForEach的多协程版本

func ParallelGroupBy added in v1.2.0

func ParallelGroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T

ParallelGroupBy GroupBy的多协程版本

func ParallelMap added in v1.2.0

func ParallelMap[T any, R any](collection []T, iteratee func(item T, index int) R) []R

ParallelMap Map的多协程版本

func ParallelPartitionBy added in v1.2.0

func ParallelPartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [][]T

ParallelPartitionBy PartitionBy的多协程版本

func ParallelTimes added in v1.2.0

func ParallelTimes[T any](count int, iteratee func(index int) T) []T

ParallelTimes Times的多携程版本

func PartitionBy added in v1.2.0

func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [][]T

PartitionBy 和GroupBy类似,返回拆分为组的元素切片.分组是通过iteratee运行切片的每个元素的结果生成的.

func Range added in v1.2.0

func Range(elementNum int) []int

Range 根据给定的长度创建切片 Play: https://go.dev/play/p/0r6VimXAi9H

func RangeFrom added in v1.2.0

func RangeFrom[T Integer | Float](start T, elementNum int) []T

RangeFrom 根据给定的其实数值和长度创建切片 Play: https://go.dev/play/p/0r6VimXAi9H

func RangeWithStep added in v1.2.0

func RangeWithStep[T Integer | Float](start, end, step T) []T

RangeWithStep 给定开始值和结束值,按照步长生成切片 @param: start 开始值 @param: end 结束值 @param: step 步长

func Reduce added in v1.2.0

func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R

Reduce 将切片缩减为一个值,该值是通过累加器运行切片中每个元素的累积结果,其中每次连续调用都提供前一次调用的返回值. 压缩 collection(切片)为一个值,通过 iteratee(迭代函数)遍历 collection(切片)中的每个元素,每次返回的值会作为下一次迭代使用(注:作为iteratee(迭代函数)的第一个参数使用). @param: collection切片 @param: accumulator执行计算的函数,agg:每一次计算的初始值,item:切片的元素,index:切片的元素索引 @param: initial初始值

func ReduceRight added in v1.2.0

func ReduceRight[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R

ReduceRight 从尾向头开始reduce操作 Play: https://go.dev/play/p/Fq3W70l7wXF

func Reject added in v1.2.0

func Reject[V any](collection []V, predicate func(item V, index int) bool) []V

Reject 是Filter的相反切片,排除掉返回结果为true的元素 Play: https://go.dev/play/p/YkLMODy1WEL

func RemoveAllItem

func RemoveAllItem[T any](s []T, compare func(T) bool) []T

RemoveAllItem 从切片中移除所有匹配的元素

func RemoveFirstItem

func RemoveFirstItem[T any](s []T, compare func(T) bool) []T

RemoveFirstItem 从切片中移除第一个匹配元素

func Repeat added in v1.2.0

func Repeat[T Cloneable[T]](count int, initial T) []T

Repeat 构建包含N个T元素副本的切片 Play: https://go.dev/play/p/g3uHXbmc3b6

func RepeatBy added in v1.2.0

func RepeatBy[T any](count int, predicate func(index int) T) []T

RepeatBy 根据函数predicate构建包含N个副本的切片. Play: https://go.dev/play/p/ozZLCtX_hNU

func Replace added in v1.2.0

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

Replace 替换切片里的数据 @param: collection 原切片数据 @param: old 待替换的旧数据 @param: new 替换的新数据 @param: n 替换的次数 Play: https://go.dev/play/p/XfPzmf9gql6

func ReplaceAll added in v1.2.0

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

ReplaceAll 替换切片里所有匹配的数据 @param: collection 原切片数据 @param: old 待替换的旧数据 @param: new 替换的新数据 Play: https://go.dev/play/p/a9xZFUHfYcV

func Reverse added in v1.2.0

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

Reverse 翻转切片

func Shuffle added in v1.2.0

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

Shuffle 创建一个被打乱值的集合.使用Fisher-Yates shuffle算法。

func Slice added in v1.2.0

func Slice[T any](collection []T, start int, end int) []T

Slice 从开始位置和结束位置截取切片 @param: collection 原切片数据 @param: start 开始位置,包含 @param: end 结束位置,不包含 Play: https://go.dev/play/p/8XWYhfMMA1h

func SliceDiff

func SliceDiff[T any](s1 []T, s2 []T, compare func(T, T) bool) []T

SliceDiff 2个切片的差集

func SliceIntersect

func SliceIntersect[T any](s1 []T, s2 []T, compare func(T, T) bool) []T

SliceIntersect 2个切片的交集

func SliceToMap added in v1.2.0

func SliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V

SliceToMap Associate的别名版本 Play: https://go.dev/play/p/WHa2CfMO3Lr

func Some added in v1.2.0

func Some[T comparable](collection []T, subset []T) bool

Some 判断一个切片是否至少包含另一个切片的一个元素(和子集的元素有重叠) @param: collection 大切片 @param: subset 小切片

func SomeBy added in v1.2.0

func SomeBy[T any](collection []T, predicate func(item T) bool) bool

SomeBy 切片元素经过函数计算,结果至少有一个true @param: collection 大切片 @param: predicate 计算函数

func Subset added in v1.2.0

func Subset[T any](collection []T, offset int, length uint) []T

Subset 从指定位置截取切片的指定长度 @param: collection 原切片数据 @param: offset 开始位置,包含 @param: length 截取长度 Play: https://go.dev/play/p/tOQu1GhFcog

func Sum added in v1.2.0

func Sum[T Float | Integer | Complex](collection []T) T

Sum 对切片元素的值进行累计 Play: https://go.dev/play/p/upfeJVqs4Bt

func SumBy added in v1.2.0

func SumBy[T any, R Float | Integer | Complex](collection []T, iteratee func(item T) R) R

SumBy 对切片的元素进行计算,计算的结果进行累加 Play: https://go.dev/play/p/Dz_a_7jN_ca

func Times added in v1.2.0

func Times[T any](count int, iteratee func(index int) T) []T

Times 调用函数iteratee指定次数,返回每次调用结果的数组.

func ToSlicePtr added in v1.2.0

func ToSlicePtr[T any](collection []T) []*T

ToSlicePtr 返回切片里元素指针的新切片

func Union added in v1.2.0

func Union[T comparable](lists ...[]T) []T

Union 计算2个集合的并集

func Uniq added in v1.2.0

func Uniq[T comparable](collection []T) []T

Uniq 返回数组的无重复版本,其中只保留每个元素的第一次出现.

func UniqBy added in v1.2.0

func UniqBy[T any, U comparable](collection []T, iteratee func(item T) U) []T

UniqBy 返回数组的无重复版本,其中只保留每个元素的第一次出现.它接受数组中每个元素调用的iteratee,以生成计算惟一性的条件.

func Without added in v1.2.0

func Without[T comparable](collection []T, exclude ...T) []T

Without 将给定的排除元素从切片移除

func WithoutEmpty added in v1.2.0

func WithoutEmpty[T comparable](collection []T) []T

WithoutEmpty 排除切片里面的空数据

Types

type Cloneable added in v1.2.0

type Cloneable[T any] interface {
	Clone() T
}

Cloneable defines a constraint of types having Clone() T method.

type Complex added in v1.2.0

type Complex interface {
	~complex64 | ~complex128
}

type Float added in v1.2.0

type Float interface {
	~float32 | ~float64
}

type Integer added in v1.2.0

type Integer interface {
	Signed | Unsigned
}

type Ordered added in v1.2.0

type Ordered interface {
	Integer | Float | ~string
}

type Signed added in v1.2.0

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned added in v1.2.0

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Jump to

Keyboard shortcuts

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