Documentation ¶
Overview ¶
Package collection 定义了各种对于集合操作有用的各种函数
Index ¶
- func AllInComparableSlice[S ~[]V, V comparable](slice S, values []V) bool
- func AllInComparableSlices[S ~[]V, V comparable](slices []S, values []V) bool
- func AllInSlice[S ~[]V, V any](slice S, values []V, handler ComparisonHandler[V]) bool
- func AllInSlices[S ~[]V, V any](slices []S, values []V, handler ComparisonHandler[V]) bool
- func AllKeyInMap[M ~map[K]V, K comparable, V any](m M, keys ...K) bool
- func AllKeyInMaps[M ~map[K]V, K comparable, V any](maps []M, keys ...K) bool
- func AllValueInMap[M ~map[K]V, K comparable, V any](m M, values []V, handler ComparisonHandler[V]) bool
- func AllValueInMaps[M ~map[K]V, K comparable, V any](maps []M, values []V, handler ComparisonHandler[V]) bool
- func AnyInAllComparableSlices[S ~[]V, V comparable](slices []S, values []V) bool
- func AnyInAllSlices[S ~[]V, V any](slices []S, values []V, handler ComparisonHandler[V]) bool
- func AnyInComparableSlice[S ~[]V, V comparable](slice S, values []V) bool
- func AnyInComparableSlices[S ~[]V, V comparable](slices []S, values []V) bool
- func AnyInSlice[S ~[]V, V any](slice S, values []V, handler ComparisonHandler[V]) bool
- func AnyInSlices[S ~[]V, V any](slices []S, values []V, handler ComparisonHandler[V]) bool
- func AnyKeyInAllMaps[M ~map[K]V, K comparable, V any](maps []M, keys []K) bool
- func AnyKeyInMap[M ~map[K]V, K comparable, V any](m M, keys ...K) bool
- func AnyKeyInMaps[M ~map[K]V, K comparable, V any](maps []M, keys ...K) bool
- func AnyValueInMap[M ~map[K]V, K comparable, V any](m M, values []V, handler ComparisonHandler[V]) bool
- func AnyValueInMaps[M ~map[K]V, K comparable, V any](maps []M, values []V, handler ComparisonHandler[V]) bool
- func Asc[S ~[]V, V any, Sort generic.Ordered](slice *S, getter func(index int) Sort)
- func AscBy[Sort generic.Ordered](a, b Sort) bool
- func AscByClone[S ~[]V, V any, Sort generic.Ordered](slice S, getter func(index int) Sort) S
- func ChooseRandomIndex[S ~[]V, V any](slice S) (index int)
- func ChooseRandomIndexN[S ~[]V, V any](slice S, n int) (result []int)
- func ChooseRandomIndexRepeatN[S ~[]V, V any](slice S, n int) (result []int)
- func ChooseRandomMapKey[M ~map[K]V, K comparable, V any](m M) (k K)
- func ChooseRandomMapKeyAndValue[M ~map[K]V, K comparable, V any](m M) (k K, v V)
- func ChooseRandomMapKeyAndValueN[M ~map[K]V, K comparable, V any](m M, n int) M
- func ChooseRandomMapKeyAndValueRepeatN[M ~map[K]V, K comparable, V any](m M, n int) M
- func ChooseRandomMapKeyN[M ~map[K]V, K comparable, V any](m M, n int) (result []K)
- func ChooseRandomMapKeyRepeatN[M ~map[K]V, K comparable, V any](m M, n int) (result []K)
- func ChooseRandomMapValue[M ~map[K]V, K comparable, V any](m M) (v V)
- func ChooseRandomMapValueN[M ~map[K]V, K comparable, V any](m M, n int) (result []V)
- func ChooseRandomMapValueRepeatN[M ~map[K]V, K comparable, V any](m M, n int) (result []V)
- func ChooseRandomSliceElement[S ~[]V, V any](slice S) (v V)
- func ChooseRandomSliceElementN[S ~[]V, V any](slice S, n int) (result []V)
- func ChooseRandomSliceElementRepeatN[S ~[]V, V any](slice S, n int) (result []V)
- func ClearMap[M ~map[K]V, K comparable, V any](m M)
- func ClearSlice[S ~[]V, V any](slice *S)
- func CloneMap[M ~map[K]V, K comparable, V any](m M) M
- func CloneMapN[M ~map[K]V, K comparable, V any](m M, n int) []M
- func CloneMaps[M ~map[K]V, K comparable, V any](maps ...M) []M
- func CloneSlice[S ~[]V, V any](slice S) S
- func CloneSliceN[S ~[]V, V any](slice S, n int) []S
- func CloneSlices[S ~[]V, V any](slices ...S) []S
- func ConvertMapKeysToBatches[M ~map[K]V, K comparable, V any](m M, batchSize int) [][]K
- func ConvertMapKeysToSlice[M ~map[K]V, K comparable, V any](m M) []K
- func ConvertMapValuesToBatches[M ~map[K]V, K comparable, V any](m M, batchSize int) [][]V
- func ConvertMapValuesToBool[M ~map[K]V, N map[K]bool, K comparable, V any](m M) N
- func ConvertMapValuesToSlice[M ~map[K]V, K comparable, V any](m M) []V
- func ConvertSliceToAny[S ~[]V, V any](s S) []any
- func ConvertSliceToBatches[S ~[]V, V any](s S, batchSize int) []S
- func ConvertSliceToBoolMap[S ~[]V, V comparable](s S) map[V]bool
- func ConvertSliceToIndexMap[S ~[]V, V any](s S) map[int]V
- func ConvertSliceToIndexOnlyMap[S ~[]V, V any](s S) map[int]struct{}
- func ConvertSliceToMap[S ~[]V, V comparable](s S) map[V]struct{}
- func DeduplicateSlice[S ~[]V, V comparable](s S) S
- func DeduplicateSliceInPlace[S ~[]V, V comparable](s *S)
- func DeduplicateSliceInPlaceWithCompare[S ~[]V, V any](s *S, compare func(a, b V) bool)
- func DeduplicateSliceWithCompare[S ~[]V, V any](s S, compare func(a, b V) bool) S
- func Desc[S ~[]V, V any, Sort generic.Ordered](slice *S, getter func(index int) Sort)
- func DescBy[Sort generic.Ordered](a, b Sort) bool
- func DescByClone[S ~[]V, V any, Sort generic.Ordered](slice S, getter func(index int) Sort) S
- func DropSliceByCondition[S ~[]V, V any](slice *S, condition func(v V) bool)
- func DropSliceByIndices[S ~[]V, V any](slice *S, indices ...int)
- func DropSliceOverlappingElements[S ~[]V, V any](slice *S, anotherSlice []V, comparisonHandler ComparisonHandler[V])
- func EqualComparableMap[M ~map[K]V, K comparable, V comparable](map1 M, map2 M) bool
- func EqualComparableSlice[S ~[]V, V comparable](slice1 S, slice2 S) bool
- func EqualMap[M ~map[K]V, K comparable, V any](map1 M, map2 M, handler ComparisonHandler[V]) bool
- func EqualSlice[S ~[]V, V any](slice1 S, slice2 S, handler ComparisonHandler[V]) bool
- func FilterOutByCondition[S ~[]V, V any](slice S, condition func(v V) bool) S
- func FilterOutByIndices[S []V, V any](slice S, indices ...int) S
- func FilterOutByKey[M ~map[K]V, K comparable, V any](m M, key K) M
- func FilterOutByKeys[M ~map[K]V, K comparable, V any](m M, keys ...K) M
- func FilterOutByMap[M ~map[K]V, K comparable, V any](m M, condition func(k K, v V) bool) M
- func FilterOutByValue[M ~map[K]V, K comparable, V any](m M, value V, handler ComparisonHandler[V]) M
- func FilterOutByValues[M ~map[K]V, K comparable, V any](m M, values []V, handler ComparisonHandler[V]) M
- func FindCombinationsInSliceByRange[S ~[]V, V any](s S, minSize, maxSize int) []S
- func FindFirstOrDefaultInSlice[S ~[]V, V any](slice S, defaultValue V) V
- func FindInComparableSlice[S ~[]V, V comparable](slice S, v V) (i int, t V)
- func FindInSlice[S ~[]V, V any](slice S, handler func(v V) bool) (i int, t V)
- func FindIndexInComparableSlice[S ~[]V, V comparable](slice S, v V) int
- func FindIndexInSlice[S ~[]V, V any](slice S, handler func(v V) bool) int
- func FindLoopedNextInSlice[S ~[]V, V any](slice S, i int) (next int, value V)
- func FindLoopedPrevInSlice[S ~[]V, V any](slice S, i int) (prev int, value V)
- func FindMaxFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (result V)
- func FindMaxFromMap[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, handler OrderedValueGetter[V, N]) (result V)
- func FindMaximumInComparableSlice[S ~[]V, V generic.Ordered](slice S) (result V)
- func FindMaximumInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (result V)
- func FindMin2MaxFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (min, max V)
- func FindMin2MaxFromMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (min, max V)
- func FindMin2MaxInComparableSlice[S ~[]V, V generic.Ordered](slice S) (min, max V)
- func FindMin2MaxInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (min, max V)
- func FindMinFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (result V)
- func FindMinFromMap[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, handler OrderedValueGetter[V, N]) (result V)
- func FindMinimumInComparableSlice[S ~[]V, V generic.Ordered](slice S) (result V)
- func FindMinimumInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (result V)
- func FindOrDefaultInComparableSlice[S ~[]V, V comparable](slice S, v V, defaultValue V) (t V)
- func FindOrDefaultInSlice[S ~[]V, V any](slice S, defaultValue V, handler func(v V) bool) (t V)
- func InAllComparableSlices[S ~[]V, V comparable](slices []S, v V) bool
- func InAllSlices[S ~[]V, V any](slices []S, v V, handler ComparisonHandler[V]) bool
- func InComparableSlice[S ~[]V, V comparable](slice S, v V) bool
- func InComparableSlices[S ~[]V, V comparable](slices []S, v V) bool
- func InSlice[S ~[]V, V any](slice S, v V, handler ComparisonHandler[V]) bool
- func InSlices[S ~[]V, V any](slices []S, v V, handler ComparisonHandler[V]) bool
- func InvertMap[M ~map[K]V, N map[V]K, K, V comparable](m M) N
- func KeyInAllMaps[M ~map[K]V, K comparable, V any](maps []M, key K) bool
- func KeyInMap[M ~map[K]V, K comparable, V any](m M, key K) bool
- func LoopMap[M ~map[K]V, K comparable, V any](m M, f func(i int, key K, val V) bool)
- func LoopMapByKeyGetterAsc[M ~map[K]V, K comparable, V comparable, N generic.Ordered](m M, getter func(k K) N, f func(i int, key K, val V) bool)
- func LoopMapByKeyGetterDesc[M ~map[K]V, K comparable, V comparable, N generic.Ordered](m M, getter func(k K) N, f func(i int, key K, val V) bool)
- func LoopMapByOrderedKeyAsc[M ~map[K]V, K generic.Ordered, V any](m M, f func(i int, key K, val V) bool)
- func LoopMapByOrderedKeyDesc[M ~map[K]V, K generic.Ordered, V any](m M, f func(i int, key K, val V) bool)
- func LoopMapByOrderedValueAsc[M ~map[K]V, K comparable, V generic.Ordered](m M, f func(i int, key K, val V) bool)
- func LoopMapByOrderedValueDesc[M ~map[K]V, K comparable, V generic.Ordered](m M, f func(i int, key K, val V) bool)
- func LoopMapByValueGetterAsc[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, getter func(v V) N, f func(i int, key K, val V) bool)
- func LoopMapByValueGetterDesc[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, getter func(v V) N, f func(i int, key K, val V) bool)
- func LoopSlice[S ~[]V, V any](slice S, f func(i int, val V) bool)
- func MappingFromMap[M ~map[K]V, NM map[K]N, K comparable, V, N any](m M, handler func(value V) N) NM
- func MappingFromSlice[S ~[]V, NS []N, V, N any](slice S, handler func(value V) N) NS
- func MergeMaps[M ~map[K]V, K comparable, V any](maps ...M) (result M)
- func MergeMapsWithSkip[M ~map[K]V, K comparable, V any](maps ...M) (result M)
- func MergeSlice[V any](values ...V) (result []V)
- func MergeSlices[S ~[]V, V any](slices ...S) (result S)
- func ReverseLoopSlice[S ~[]V, V any](slice S, f func(i int, val V) bool)
- func ReverseSlice[S ~[]V, V any](s *S)
- func Shuffle[S ~[]V, V any](slice *S)
- func ShuffleByClone[S ~[]V, V any](slice S) S
- func SwapSlice[S ~[]V, V any](slice *S, i, j int)
- func ValueInMap[M ~map[K]V, K comparable, V any](m M, value V, handler ComparisonHandler[V]) bool
- type ComparisonHandler
- type OrderedValueGetter
Examples ¶
- AllInComparableSlice
- AllInComparableSlices
- AllInSlice
- AllInSlices
- AllKeyInMap
- AllKeyInMaps
- AllValueInMap
- AllValueInMaps
- AnyInAllComparableSlices
- AnyInAllSlices
- AnyInComparableSlice
- AnyInComparableSlices
- AnyInSlice
- AnyInSlices
- AnyKeyInAllMaps
- AnyKeyInMap
- AnyKeyInMaps
- AnyValueInMap
- AnyValueInMaps
- Asc
- AscBy
- AscByClone
- ChooseRandomIndex
- ChooseRandomIndexN
- ChooseRandomIndexRepeatN
- ChooseRandomMapKey
- ChooseRandomMapKeyAndValue
- ChooseRandomMapKeyAndValueN
- ChooseRandomMapKeyAndValueRepeatN
- ChooseRandomMapKeyN
- ChooseRandomMapKeyRepeatN
- ChooseRandomMapValue
- ChooseRandomMapValueN
- ChooseRandomMapValueRepeatN
- ChooseRandomSliceElement
- ChooseRandomSliceElementN
- ChooseRandomSliceElementRepeatN
- ClearMap
- ClearSlice
- CloneMap
- CloneMapN
- CloneMaps
- CloneSlice
- CloneSliceN
- CloneSlices
- ConvertMapKeysToBatches
- ConvertMapKeysToSlice
- ConvertMapValuesToBatches
- ConvertMapValuesToBool
- ConvertMapValuesToSlice
- ConvertSliceToAny
- ConvertSliceToBatches
- ConvertSliceToBoolMap
- ConvertSliceToIndexMap
- ConvertSliceToIndexOnlyMap
- ConvertSliceToMap
- DeduplicateSlice
- DeduplicateSliceInPlace
- DeduplicateSliceInPlaceWithCompare
- DeduplicateSliceWithCompare
- Desc
- DescBy
- DescByClone
- DropSliceByCondition
- DropSliceByIndices
- DropSliceOverlappingElements
- EqualComparableMap
- EqualComparableSlice
- EqualMap
- EqualSlice
- FilterOutByCondition
- FilterOutByIndices
- FilterOutByKey
- FilterOutByKeys
- FilterOutByMap
- FilterOutByValue
- FilterOutByValues
- FindCombinationsInSliceByRange
- FindFirstOrDefaultInSlice
- FindInComparableSlice
- FindInSlice
- FindIndexInComparableSlice
- FindIndexInSlice
- FindLoopedNextInSlice
- FindLoopedPrevInSlice
- FindMaxFromComparableMap
- FindMaxFromMap
- FindMaximumInComparableSlice
- FindMaximumInSlice
- FindMin2MaxFromComparableMap
- FindMin2MaxFromMap
- FindMin2MaxInComparableSlice
- FindMin2MaxInSlice
- FindMinFromComparableMap
- FindMinFromMap
- FindMinimumInComparableSlice
- FindMinimumInSlice
- FindOrDefaultInComparableSlice
- FindOrDefaultInSlice
- InAllComparableSlices
- InAllSlices
- InComparableSlice
- InComparableSlices
- InSlice
- InSlices
- InvertMap
- KeyInAllMaps
- KeyInMap
- LoopMap
- LoopMapByKeyGetterAsc
- LoopMapByKeyGetterDesc
- LoopMapByOrderedKeyAsc
- LoopMapByOrderedKeyDesc
- LoopMapByOrderedValueAsc
- LoopMapByOrderedValueDesc
- LoopMapByValueGetterAsc
- LoopMapByValueGetterDesc
- LoopSlice
- MappingFromMap
- MappingFromSlice
- MergeMaps
- MergeMapsWithSkip
- MergeSlice
- MergeSlices
- ReverseLoopSlice
- ReverseSlice
- Shuffle
- ShuffleByClone
- SwapSlice
- ValueInMap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllInComparableSlice ¶
func AllInComparableSlice[S ~[]V, V comparable](slice S, values []V) bool
AllInComparableSlice 检查 values 中的所有元素是否均被包含在 slice 中
- 在所有 values 中的元素都被包含在 slice 中时,返回 true
- 当 values 长度为 0 或为 nil 时,将返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AllInComparableSlice([]int{1, 2, 3}, []int{1, 2}) fmt.Println(result) }
Output: true
func AllInComparableSlices ¶
func AllInComparableSlices[S ~[]V, V comparable](slices []S, values []V) bool
AllInComparableSlices 通过将多个切片合并后检查 values 中的所有元素是否被包含在 slices 中
- 当 values 中的所有元素都被包含在 slices 的任一成员中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AllInComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}) fmt.Println(result) }
Output: true
func AllInSlice ¶
func AllInSlice[S ~[]V, V any](slice S, values []V, handler ComparisonHandler[V]) bool
AllInSlice 检查 values 中的所有元素是否均被包含在 slice 中,当 handler 返回 true 时,表示 values 中的某个元素和 slice 中的某个元素相匹配
- 在所有 values 中的元素都被包含在 slice 中时,返回 true
- 当 values 长度为 0 或为 nil 时,将返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AllInSlice([]int{1, 2, 3}, []int{1, 2}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func AllInSlices ¶
func AllInSlices[S ~[]V, V any](slices []S, values []V, handler ComparisonHandler[V]) bool
AllInSlices 通过将多个切片合并后检查 values 中的所有元素是否被包含在 slices 中,当 handler 返回 true 时,表示 values 中的某个元素和 slices 中的某个元素相匹配
- 当 values 中的所有元素都被包含在 slices 的任一成员中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AllInSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func AllKeyInMap ¶
func AllKeyInMap[M ~map[K]V, K comparable, V any](m M, keys ...K) bool
AllKeyInMap 检查 m 中是否包含 keys 中所有的元素
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AllKeyInMap(map[string]int{"a": 1, "b": 2}, "a", "b") fmt.Println(result) }
Output: true
func AllKeyInMaps ¶
func AllKeyInMaps[M ~map[K]V, K comparable, V any](maps []M, keys ...K) bool
AllKeyInMaps 检查 maps 中的每一个元素是否均包含 keys 中所有的元素
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AllKeyInMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, "a", "b") fmt.Println(result) }
Output: true
func AllValueInMap ¶
func AllValueInMap[M ~map[K]V, K comparable, V any](m M, values []V, handler ComparisonHandler[V]) bool
AllValueInMap 检查 m 中是否包含 values 中所有的元素,当 handler 返回 true 时,表示 values 中的某个元素和 m 中的某个元素相匹配
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AllValueInMap(map[string]int{"a": 1, "b": 2}, []int{1}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func AllValueInMaps ¶
func AllValueInMaps[M ~map[K]V, K comparable, V any](maps []M, values []V, handler ComparisonHandler[V]) bool
AllValueInMaps 检查 maps 中的每一个元素是否均包含 value 中所有的元素,当 handler 返回 true 时,表示 value 中的某个元素和 maps 中的某个元素相匹配
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AllValueInMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, []int{1}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func AnyInAllComparableSlices ¶
func AnyInAllComparableSlices[S ~[]V, V comparable](slices []S, values []V) bool
AnyInAllComparableSlices 检查 slices 中的每一个元素是否均包含至少任意一个 values 中的元素
- 当 slices 中的每一个元素均包含至少任意一个 values 中的元素时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyInAllComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}) fmt.Println(result) }
Output: false
func AnyInAllSlices ¶
func AnyInAllSlices[S ~[]V, V any](slices []S, values []V, handler ComparisonHandler[V]) bool
AnyInAllSlices 检查 slices 中的每一个元素是否均包含至少任意一个 values 中的元素,当 handler 返回 true 时,表示 value 中的某个元素和 slices 中的某个元素相匹配
- 当 slices 中的每一个元素均包含至少任意一个 values 中的元素时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyInAllSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: false
func AnyInComparableSlice ¶
func AnyInComparableSlice[S ~[]V, V comparable](slice S, values []V) bool
AnyInComparableSlice 检查 values 中的任意一个元素是否被包含在 slice 中
- 当 values 中的任意一个元素被包含在 slice 中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyInComparableSlice([]int{1, 2, 3}, []int{1, 2}) fmt.Println(result) }
Output: true
func AnyInComparableSlices ¶
func AnyInComparableSlices[S ~[]V, V comparable](slices []S, values []V) bool
AnyInComparableSlices 通过将多个切片合并后检查 values 中的任意一个元素是否被包含在 slices 中
- 当 values 中的任意一个元素被包含在 slices 的任一成员中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyInComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}) fmt.Println(result) }
Output: true
func AnyInSlice ¶
func AnyInSlice[S ~[]V, V any](slice S, values []V, handler ComparisonHandler[V]) bool
AnyInSlice 检查 values 中的任意一个元素是否被包含在 slice 中,当 handler 返回 true 时,表示 value 中的某个元素和 slice 中的某个元素相匹配
- 当 values 中的任意一个元素被包含在 slice 中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyInSlice([]int{1, 2, 3}, []int{1, 2}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func AnyInSlices ¶
func AnyInSlices[S ~[]V, V any](slices []S, values []V, handler ComparisonHandler[V]) bool
AnyInSlices 通过将多个切片合并后检查 values 中的任意一个元素是否被包含在 slices 中,当 handler 返回 true 时,表示 values 中的某个元素和 slices 中的某个元素相匹配
- 当 values 中的任意一个元素被包含在 slices 的任一成员中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyInSlices([][]int{{1, 2, 3}, {4, 5, 6}}, []int{1, 2}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func AnyKeyInAllMaps ¶
func AnyKeyInAllMaps[M ~map[K]V, K comparable, V any](maps []M, keys []K) bool
AnyKeyInAllMaps 检查 maps 中的每一个元素是否均包含 keys 中任意一个元素
- 当 maps 中的每一个元素均包含 keys 中任意一个元素时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyKeyInAllMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, []string{"a"}) fmt.Println(result) }
Output: true
func AnyKeyInMap ¶
func AnyKeyInMap[M ~map[K]V, K comparable, V any](m M, keys ...K) bool
AnyKeyInMap 检查 m 中是否包含 keys 中任意一个元素
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyKeyInMap(map[string]int{"a": 1, "b": 2}, "a", "b") fmt.Println(result) }
Output: true
func AnyKeyInMaps ¶
func AnyKeyInMaps[M ~map[K]V, K comparable, V any](maps []M, keys ...K) bool
AnyKeyInMaps 检查 keys 中的任意一个元素是否被包含在 maps 中的任意一个元素中
- 当 keys 中的任意一个元素被包含在 maps 中的任意一个元素中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyKeyInMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, "a", "b") fmt.Println(result) }
Output: true
func AnyValueInMap ¶
func AnyValueInMap[M ~map[K]V, K comparable, V any](m M, values []V, handler ComparisonHandler[V]) bool
AnyValueInMap 检查 m 中是否包含 values 中任意一个元素,当 handler 返回 true 时,表示 values 中的某个元素和 m 中的某个元素相匹配
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyValueInMap(map[string]int{"a": 1, "b": 2}, []int{1}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func AnyValueInMaps ¶
func AnyValueInMaps[M ~map[K]V, K comparable, V any](maps []M, values []V, handler ComparisonHandler[V]) bool
AnyValueInMaps 检查 maps 中的任意一个元素是否包含 value 中的任意一个元素,当 handler 返回 true 时,表示 value 中的某个元素和 maps 中的某个元素相匹配
- 当 maps 中的任意一个元素包含 value 中的任意一个元素时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.AnyValueInMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, []int{1}, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func Asc ¶
Asc 对切片进行升序排序
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3} collection.Asc(&slice, func(index int) int { return slice[index] }) fmt.Println(slice) }
Output: [1 2 3]
func AscBy ¶
AscBy 返回升序比较结果
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" "sort" ) func main() { var slice = []int{1, 2, 3} sort.Slice(slice, func(i, j int) bool { return collection.AscBy(slice[i], slice[j]) }) fmt.Println(slice) }
Output: [1 2 3]
func AscByClone ¶
AscByClone 对切片进行升序排序,返回排序后的切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3} result := collection.AscByClone(slice, func(index int) int { return slice[index] }) fmt.Println(result) }
Output: [1 2 3]
func ChooseRandomIndex ¶
ChooseRandomIndex 返回 slice 中随机一个元素的索引,当 slice 长度为 0 时将会得到 -1
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomIndex([]int{1}) fmt.Println(result) }
Output: 0
func ChooseRandomIndexN ¶
ChooseRandomIndexN 获取切片中的 n 个随机元素的索引
- 如果 n 大于切片长度或小于 0 时将会发生 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomIndexN([]int{1}, 1) fmt.Println(result) }
Output: [0]
func ChooseRandomIndexRepeatN ¶
ChooseRandomIndexRepeatN 返回 slice 中的 n 个可重复随机元素的索引
- 当 slice 长度为 0 或 n 小于等于 0 时将会返回 nil
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomIndexRepeatN([]int{1}, 10) fmt.Println(result) }
Output: [0 0 0 0 0 0 0 0 0 0]
func ChooseRandomMapKey ¶
func ChooseRandomMapKey[M ~map[K]V, K comparable, V any](m M) (k K)
ChooseRandomMapKey 获取 map 中的随机 key
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomMapKey(map[int]int{1: 1}) fmt.Println(result) }
Output: 1
func ChooseRandomMapKeyAndValue ¶
func ChooseRandomMapKeyAndValue[M ~map[K]V, K comparable, V any](m M) (k K, v V)
ChooseRandomMapKeyAndValue 获取 map 中的随机 key 和 v
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { k, v := collection.ChooseRandomMapKeyAndValue(map[int]int{1: 1}) fmt.Println(k, v) }
Output: 1 1
func ChooseRandomMapKeyAndValueN ¶
func ChooseRandomMapKeyAndValueN[M ~map[K]V, K comparable, V any](m M, n int) M
ChooseRandomMapKeyAndValueN 获取 map 中的 inputN 个随机 key 和 v
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomMapKeyAndValueN(map[int]int{1: 1}, 1) fmt.Println(result) }
Output: map[1:1]
func ChooseRandomMapKeyAndValueRepeatN ¶
func ChooseRandomMapKeyAndValueRepeatN[M ~map[K]V, K comparable, V any](m M, n int) M
ChooseRandomMapKeyAndValueRepeatN 获取 map 中的 n 个随机 key 和 v,允许重复
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomMapKeyAndValueRepeatN(map[int]int{1: 1}, 1) fmt.Println(result) }
Output: map[1:1]
func ChooseRandomMapKeyN ¶
func ChooseRandomMapKeyN[M ~map[K]V, K comparable, V any](m M, n int) (result []K)
ChooseRandomMapKeyN 获取 map 中的 inputN 个随机 key
- 如果 inputN 大于 map 长度或小于 0 时将会发生 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomMapKeyN(map[int]int{1: 1}, 1) fmt.Println(result) }
Output: [1]
func ChooseRandomMapKeyRepeatN ¶
func ChooseRandomMapKeyRepeatN[M ~map[K]V, K comparable, V any](m M, n int) (result []K)
ChooseRandomMapKeyRepeatN 获取 map 中的 n 个随机 key,允许重复
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomMapKeyRepeatN(map[int]int{1: 1}, 1) fmt.Println(result) }
Output: [1]
func ChooseRandomMapValue ¶
func ChooseRandomMapValue[M ~map[K]V, K comparable, V any](m M) (v V)
ChooseRandomMapValue 获取 map 中的随机 value
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomMapValue(map[int]int{1: 1}) fmt.Println(result) }
Output: 1
func ChooseRandomMapValueN ¶
func ChooseRandomMapValueN[M ~map[K]V, K comparable, V any](m M, n int) (result []V)
ChooseRandomMapValueN 获取 map 中的 n 个随机 value
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomMapValueN(map[int]int{1: 1}, 1) fmt.Println(result) }
Output: [1]
func ChooseRandomMapValueRepeatN ¶
func ChooseRandomMapValueRepeatN[M ~map[K]V, K comparable, V any](m M, n int) (result []V)
ChooseRandomMapValueRepeatN 获取 map 中的 n 个随机 n,允许重复
- 如果 n 大于 map 长度或小于 0 时将会发生 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomMapValueRepeatN(map[int]int{1: 1}, 1) fmt.Println(result) }
Output: [1]
func ChooseRandomSliceElement ¶
func ChooseRandomSliceElement[S ~[]V, V any](slice S) (v V)
ChooseRandomSliceElement 返回 slice 中随机一个元素,当 slice 长度为 0 时将会得到 V 的零值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomSliceElement([]int{1}) fmt.Println(result) }
Output: 1
func ChooseRandomSliceElementN ¶
ChooseRandomSliceElementN 返回 slice 中的 n 个不可重复的随机元素
- 当 slice 长度为 0 或 n 大于 slice 长度或小于 0 时将会发生 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomSliceElementN([]int{1}, 1) fmt.Println(result) }
Output: [1]
func ChooseRandomSliceElementRepeatN ¶
ChooseRandomSliceElementRepeatN 返回 slice 中的 n 个可重复随机元素
- 当 slice 长度为 0 或 n 小于等于 0 时将会返回 nil
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ChooseRandomSliceElementRepeatN([]int{1}, 10) fmt.Println(result) }
Output: [1 1 1 1 1 1 1 1 1 1]
func ClearMap ¶
func ClearMap[M ~map[K]V, K comparable, V any](m M)
ClearMap 清空 map
Example ¶
m := map[int]int{1: 1, 2: 2, 3: 3} ClearMap(m) fmt.Println(m)
Output: map[]
func ClearSlice ¶
func ClearSlice[S ~[]V, V any](slice *S)
ClearSlice 清空切片
Example ¶
slice := []int{1, 2, 3, 4, 5} ClearSlice(&slice) fmt.Println(slice)
Output: []
func CloneMap ¶
func CloneMap[M ~map[K]V, K comparable, V any](m M) M
CloneMap 通过创建一个新 map 并将 m 的元素复制到新 map 的方式来克隆 map
- 当 m 为空时,将会返回 nil
Example ¶
在该示例中,将 map 克隆后将会得到一个新的 map result,而 result 和 map 将不会有任何关联,但是如果 map 中的元素是引用类型,那么 result 中的元素将会和 map 中的元素指向同一个地址
- 示例中的结果将会输出 3
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[int]int{1: 1, 2: 2, 3: 3} var result = collection.CloneMap(m) fmt.Println(len(result)) }
Output: 3
func CloneMapN ¶
func CloneMapN[M ~map[K]V, K comparable, V any](m M, n int) []M
CloneMapN 通过创建一个新 map 并将 m 的元素复制到新 map 的方式来克隆 map 为 n 个 map
- 当 m 为空时,将会返回 nil,当 n <= 0 时,将会返回零值切片
Example ¶
通过将 map 克隆为 2 个新的 map,将会得到一个新的 map result,而 result 和 map 将不会有任何关联,但是如果 map 中的元素是引用类型,那么 result 中的元素将会和 map 中的元素指向同一个地址
- result 的结果为 [map[1:1 2:2 3:3] map[1:1 2:2 3:3]] `无序的 Key-Value 对`
- 示例中的结果将会输出 2
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[int]int{1: 1, 2: 2, 3: 3} var result = collection.CloneMapN(m, 2) fmt.Println(len(result)) }
Output: 2
func CloneMaps ¶
func CloneMaps[M ~map[K]V, K comparable, V any](maps ...M) []M
CloneMaps 对 maps 中的每一项元素进行克隆,最终返回一个新的 map 切片
- 当 maps 为空时,将会返回 nil
- 该函数相当于使用 CloneMap 函数一次性对多个 map 进行克隆
Example ¶
通过将多个 map 克隆为 2 个新的 map,将会得到一个新的 map result,而 result 和 map 将不会有任何关联,但是如果 map 中的元素是引用类型,那么 result 中的元素将会和 map 中的元素指向同一个地址
- result 的结果为 [map[1:1 2:2 3:3] map[1:1 2:2 3:3]] `无序的 Key-Value 对`
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m1 = map[int]int{1: 1, 2: 2, 3: 3} var m2 = map[int]int{1: 1, 2: 2, 3: 3} var result = collection.CloneMaps(m1, m2) fmt.Println(len(result)) }
Output: 2
func CloneSlice ¶
func CloneSlice[S ~[]V, V any](slice S) S
CloneSlice 通过创建一个新切片并将 slice 的元素复制到新切片的方式来克隆切片
Example ¶
在该示例中,将 slice 克隆后将会得到一个新的 slice result,而 result 和 slice 将不会有任何关联,但是如果 slice 中的元素是引用类型,那么 result 中的元素将会和 slice 中的元素指向同一个地址
- 示例中的结果将会输出 [1 2 3]
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3} var result = collection.CloneSlice(slice) fmt.Println(result) }
Output: [1 2 3]
func CloneSliceN ¶
CloneSliceN 通过创建一个新切片并将 slice 的元素复制到新切片的方式来克隆切片为 n 个切片
- 当 slice 为空时,将会返回 nil,当 n <= 0 时,将会返回零值切片
Example ¶
通过将 slice 克隆为 2 个新的 slice,将会得到一个新的 slice result,而 result 和 slice 将不会有任何关联,但是如果 slice 中的元素是引用类型,那么 result 中的元素将会和 slice 中的元素指向同一个地址
- result 的结果为 [[1 2 3] [1 2 3]]
- 示例中的结果将会输出 2
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3} var result = collection.CloneSliceN(slice, 2) fmt.Println(len(result)) }
Output: 2
func CloneSlices ¶
func CloneSlices[S ~[]V, V any](slices ...S) []S
CloneSlices 对 slices 中的每一项元素进行克隆,最终返回一个新的二维切片
- 当 slices 为空时,将会返回 nil
- 该函数相当于使用 CloneSlice 函数一次性对多个切片进行克隆
Example ¶
通过将多个 slice 克隆为 2 个新的 slice,将会得到一个新的 slice result,而 result 和 slice 将不会有任何关联,但是如果 slice 中的元素是引用类型,那么 result 中的元素将会和 slice 中的元素指向同一个地址
- result 的结果为 [[1 2 3] [1 2 3]]
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice1 = []int{1, 2, 3} var slice2 = []int{1, 2, 3} var result = collection.CloneSlices(slice1, slice2) fmt.Println(len(result)) }
Output: 2
func ConvertMapKeysToBatches ¶
func ConvertMapKeysToBatches[M ~map[K]V, K comparable, V any](m M, batchSize int) [][]K
ConvertMapKeysToBatches 将映射的键转换为分批次的切片,当 batchSize 小于等于 0 或者 m 长度为 0 时,将会返回 nil
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ConvertMapKeysToBatches(map[int]int{1: 1, 2: 2, 3: 3}, 2) fmt.Println(len(result)) }
Output: 2
func ConvertMapKeysToSlice ¶
func ConvertMapKeysToSlice[M ~map[K]V, K comparable, V any](m M) []K
ConvertMapKeysToSlice 将映射的键转换为切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" "sort" ) func main() { result := collection.ConvertMapKeysToSlice(map[int]int{1: 1, 2: 2, 3: 3}) sort.Ints(result) for i, v := range result { fmt.Println(i, v) } }
Output: 0 1 1 2 2 3
func ConvertMapValuesToBatches ¶
func ConvertMapValuesToBatches[M ~map[K]V, K comparable, V any](m M, batchSize int) [][]V
ConvertMapValuesToBatches 将映射的值转换为分批次的切片,当 batchSize 小于等于 0 或者 m 长度为 0 时,将会返回 nil
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ConvertMapValuesToBatches(map[int]int{1: 1, 2: 2, 3: 3}, 2) fmt.Println(len(result)) }
Output: 2
func ConvertMapValuesToBool ¶
func ConvertMapValuesToBool[M ~map[K]V, N map[K]bool, K comparable, V any](m M) N
ConvertMapValuesToBool 将映射的值转换为布尔值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ConvertMapValuesToBool(map[int]int{1: 1}) fmt.Println(result) }
Output: map[1:true]
func ConvertMapValuesToSlice ¶
func ConvertMapValuesToSlice[M ~map[K]V, K comparable, V any](m M) []V
ConvertMapValuesToSlice 将映射的值转换为切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ConvertMapValuesToSlice(map[int]int{1: 1, 2: 2, 3: 3}) expected := map[int]bool{1: true, 2: true, 3: true} for _, v := range result { fmt.Println(expected[v]) } }
Output: true true true
func ConvertSliceToAny ¶
ConvertSliceToAny 将切片转换为任意类型的切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" "reflect" ) func main() { result := collection.ConvertSliceToAny([]int{1, 2, 3}) fmt.Println(reflect.TypeOf(result).String(), len(result)) }
Output: []interface {} 3
func ConvertSliceToBatches ¶
ConvertSliceToBatches 将切片 s 转换为分批次的切片,当 batchSize 小于等于 0 或者 s 长度为 0 时,将会返回 nil
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ConvertSliceToBatches([]int{1, 2, 3}, 2) for _, v := range result { fmt.Println(v) } }
Output: [1 2] [3]
func ConvertSliceToBoolMap ¶
func ConvertSliceToBoolMap[S ~[]V, V comparable](s S) map[V]bool
ConvertSliceToBoolMap 将切片转换为值为键的映射
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { slice := []int{1, 2, 3} result := collection.ConvertSliceToBoolMap(slice) for _, v := range slice { fmt.Println(v, result[v]) } }
Output: 1 true 2 true 3 true
func ConvertSliceToIndexMap ¶
ConvertSliceToIndexMap 将切片转换为索引为键的映射
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { slice := []int{1, 2, 3} result := collection.ConvertSliceToIndexMap(slice) for i, v := range slice { fmt.Println(result[i], v) } }
Output: 1 1 2 2 3 3
func ConvertSliceToIndexOnlyMap ¶
ConvertSliceToIndexOnlyMap 将切片转换为索引为键的映射
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { slice := []int{1, 2, 3} result := collection.ConvertSliceToIndexOnlyMap(slice) expected := map[int]bool{0: true, 1: true, 2: true} for k := range result { fmt.Println(expected[k]) } }
Output: true true true
func ConvertSliceToMap ¶
func ConvertSliceToMap[S ~[]V, V comparable](s S) map[V]struct{}
ConvertSliceToMap 将切片转换为值为键的映射
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { slice := []int{1, 2, 3} result := collection.ConvertSliceToMap(slice) fmt.Println(collection.AllKeyInMap(result, slice...)) }
Output: true
func DeduplicateSlice ¶
func DeduplicateSlice[S ~[]V, V comparable](s S) S
DeduplicateSlice 去除切片中的重复元素,返回新切片
Example ¶
slice := []int{1, 2, 3, 4, 5, 5, 4, 3, 2, 1} fmt.Println(DeduplicateSlice(slice))
Output: [1 2 3 4 5]
func DeduplicateSliceInPlace ¶
func DeduplicateSliceInPlace[S ~[]V, V comparable](s *S)
DeduplicateSliceInPlace 去除切片中的重复元素
Example ¶
slice := []int{1, 2, 3, 4, 5, 5, 4, 3, 2, 1} DeduplicateSliceInPlace(&slice) fmt.Println(slice)
Output: [1 2 3 4 5]
func DeduplicateSliceInPlaceWithCompare ¶
DeduplicateSliceInPlaceWithCompare 去除切片中的重复元素,使用自定义的比较函数
Example ¶
slice := []int{1, 2, 3, 4, 5, 5, 4, 3, 2, 1} DeduplicateSliceInPlaceWithCompare(&slice, func(a, b int) bool { return a == b }) fmt.Println(slice)
Output: [1 2 3 4 5]
func DeduplicateSliceWithCompare ¶
DeduplicateSliceWithCompare 去除切片中的重复元素,使用自定义的比较函数,返回新的切片
Example ¶
slice := []int{1, 2, 3, 4, 5, 5, 4, 3, 2, 1} fmt.Println(DeduplicateSliceWithCompare(slice, func(a, b int) bool { return a == b }))
Output: [1 2 3 4 5]
func Desc ¶
Desc 对切片进行降序排序
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3} collection.Desc(&slice, func(index int) int { return slice[index] }) fmt.Println(slice) }
Output: [3 2 1]
func DescBy ¶
DescBy 返回降序比较结果
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" "sort" ) func main() { var slice = []int{1, 2, 3} sort.Slice(slice, func(i, j int) bool { return collection.DescBy(slice[i], slice[j]) }) fmt.Println(slice) }
Output: [3 2 1]
func DescByClone ¶
DescByClone 对切片进行降序排序,返回排序后的切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3} result := collection.DescByClone(slice, func(index int) int { return slice[index] }) fmt.Println(result) }
Output: [3 2 1]
func DropSliceByCondition ¶
DropSliceByCondition 删除切片中符合条件的元素
- condition 的返回值为 true 时,将会删除该元素
Example ¶
slice := []int{1, 2, 3, 4, 5} DropSliceByCondition(&slice, func(v int) bool { return v%2 == 0 }) fmt.Println(slice)
Output: [1 3 5]
func DropSliceByIndices ¶
DropSliceByIndices 删除切片中特定索引的元素
Example ¶
slice := []int{1, 2, 3, 4, 5} DropSliceByIndices(&slice, 1, 3) fmt.Println(slice)
Output: [1 3 5]
func DropSliceOverlappingElements ¶
func DropSliceOverlappingElements[S ~[]V, V any](slice *S, anotherSlice []V, comparisonHandler ComparisonHandler[V])
DropSliceOverlappingElements 删除切片中与另一个切片重叠的元素
Example ¶
slice := []int{1, 2, 3, 4, 5} DropSliceOverlappingElements(&slice, []int{1, 3, 5}, func(source, target int) bool { return source == target }) fmt.Println(slice)
Output: [2 4]
func EqualComparableMap ¶
func EqualComparableMap[M ~map[K]V, K comparable, V comparable](map1 M, map2 M) bool
EqualComparableMap 检查两个 map 的值是否相同
- 当两个 map 的容量不同时,不会影响最终的比较结果
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { m1 := map[string]int{"a": 1, "b": 2} m2 := map[string]int{"a": 1} m3 := map[string]int{"a": 1, "b": 2} fmt.Println(collection.EqualComparableMap(m1, m2)) fmt.Println(collection.EqualComparableMap(m1, m3)) }
Output: false true
func EqualComparableSlice ¶
func EqualComparableSlice[S ~[]V, V comparable](slice1 S, slice2 S) bool
EqualComparableSlice 检查两个切片的值是否相同
- 当两个切片的容量不同时,不会影响最终的比较结果
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { s1 := []int{1, 2, 3} s2 := []int{1} s3 := []int{1, 2, 3} fmt.Println(collection.EqualComparableSlice(s1, s2)) fmt.Println(collection.EqualComparableSlice(s1, s3)) }
Output: false true
func EqualMap ¶
func EqualMap[M ~map[K]V, K comparable, V any](map1 M, map2 M, handler ComparisonHandler[V]) bool
EqualMap 检查两个 map 是否相等,当 handler 返回 true 时,表示 map1 中的某个元素和 map2 中的某个元素相匹配
- 当两个 map 的容量不同时,不会影响最终的比较结果
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { m1 := map[string]int{"a": 1, "b": 2} m2 := map[string]int{"a": 1} m3 := map[string]int{"a": 1, "b": 2} fmt.Println(collection.EqualMap(m1, m2, func(source, target int) bool { return source == target })) fmt.Println(collection.EqualMap(m1, m3, func(source, target int) bool { return source == target })) }
Output: false true
func EqualSlice ¶
func EqualSlice[S ~[]V, V any](slice1 S, slice2 S, handler ComparisonHandler[V]) bool
EqualSlice 检查两个切片是否相等,当 handler 返回 true 时,表示 slice1 中的某个元素和 slice2 中的某个元素相匹配
- 当两个切片的容量不同时,不会影响最终的比较结果
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { s1 := []int{1, 2, 3} s2 := []int{1} s3 := []int{1, 2, 3} fmt.Println(collection.EqualSlice(s1, s2, func(source, target int) bool { return source == target })) fmt.Println(collection.EqualSlice(s1, s3, func(source, target int) bool { return source == target })) }
Output: false true
func FilterOutByCondition ¶
FilterOutByCondition 过滤切片中符合条件的元素,返回过滤后的切片
- condition 的返回值为 true 时,将会过滤掉该元素
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3, 4, 5} var result = collection.FilterOutByCondition(slice, func(v int) bool { return v%2 == 0 }) fmt.Println(result) }
Output: [1 3 5]
func FilterOutByIndices ¶
FilterOutByIndices 过滤切片中特定索引的元素,返回过滤后的切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3, 4, 5} var result = collection.FilterOutByIndices(slice, 1, 3) fmt.Println(result) }
Output: [1 3 5]
func FilterOutByKey ¶
func FilterOutByKey[M ~map[K]V, K comparable, V any](m M, key K) M
FilterOutByKey 过滤 map 中特定的 key,返回过滤后的 map
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result = collection.FilterOutByKey(m, "b") fmt.Println(result) }
Output: map[a:1 c:3]
func FilterOutByKeys ¶
func FilterOutByKeys[M ~map[K]V, K comparable, V any](m M, keys ...K) M
FilterOutByKeys 过滤 map 中多个 key,返回过滤后的 map
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result = collection.FilterOutByKeys(m, "a", "c") fmt.Println(result) }
Output: map[b:2]
func FilterOutByMap ¶
func FilterOutByMap[M ~map[K]V, K comparable, V any](m M, condition func(k K, v V) bool) M
FilterOutByMap 过滤 map 中符合条件的元素,返回过滤后的 map
- condition 的返回值为 true 时,将会过滤掉该元素
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result = collection.FilterOutByMap(m, func(k string, v int) bool { return k == "a" || v == 3 }) fmt.Println(result) }
Output: map[b:2]
func FilterOutByValue ¶
func FilterOutByValue[M ~map[K]V, K comparable, V any](m M, value V, handler ComparisonHandler[V]) M
FilterOutByValue 过滤 map 中特定的 value,返回过滤后的 map
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result = collection.FilterOutByValue(m, 2, func(source, target int) bool { return source == target }) fmt.Println(len(result)) }
Output: 2
func FilterOutByValues ¶
func FilterOutByValues[M ~map[K]V, K comparable, V any](m M, values []V, handler ComparisonHandler[V]) M
FilterOutByValues 过滤 map 中多个 values,返回过滤后的 map
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result = collection.FilterOutByValues(m, []int{1}, func(source, target int) bool { return source == target }) for i, s := range []string{"a", "b", "c"} { fmt.Println(i, result[s]) } }
Output: 0 0 1 2 2 3
func FindCombinationsInSliceByRange ¶
FindCombinationsInSliceByRange 获取给定数组的所有组合,且每个组合的成员数量限制在指定范围内
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindCombinationsInSliceByRange([]int{1, 2, 3}, 1, 2) fmt.Println(len(result)) }
Output: 6
func FindFirstOrDefaultInSlice ¶
func FindFirstOrDefaultInSlice[S ~[]V, V any](slice S, defaultValue V) V
FindFirstOrDefaultInSlice 判断切片中是否存在元素,返回第一个元素,不存在则返回默认值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindFirstOrDefaultInSlice([]int{1, 2, 3}, 0) fmt.Println(result) }
Output: 1
func FindInComparableSlice ¶
func FindInComparableSlice[S ~[]V, V comparable](slice S, v V) (i int, t V)
FindInComparableSlice 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则索引返回 -1
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { index, result := collection.FindInComparableSlice([]int{1, 2, 3}, 2) fmt.Println(index, result) }
Output: 1 2
func FindInSlice ¶
FindInSlice 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则索引返回 -1
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { _, result := collection.FindInSlice([]int{1, 2, 3}, func(v int) bool { return v == 2 }) fmt.Println(result) }
Output: 2
func FindIndexInComparableSlice ¶
func FindIndexInComparableSlice[S ~[]V, V comparable](slice S, v V) int
FindIndexInComparableSlice 判断切片中是否存在某个元素,返回第一个匹配的索引,不存在则索引返回 -1
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindIndexInComparableSlice([]int{1, 2, 3}, 2) fmt.Println(result) }
Output: 1
func FindIndexInSlice ¶
FindIndexInSlice 判断切片中是否存在某个元素,返回第一个匹配的索引,不存在则索引返回 -1
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindIndexInSlice([]int{1, 2, 3}, func(v int) bool { return v == 2 }) fmt.Println(result) }
Output: 1
func FindLoopedNextInSlice ¶
FindLoopedNextInSlice 返回 i 的下一个数组成员,当 i 达到数组长度时从 0 开始
- 当 i 为负数时将返回第一个元素
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { next, v := collection.FindLoopedNextInSlice([]int{1, 2, 3}, 1) fmt.Println(next, v) }
Output: 2 3
func FindLoopedPrevInSlice ¶
FindLoopedPrevInSlice 返回 i 的上一个数组成员,当 i 为 0 时从数组末尾开始
- 当 i 为负数时将返回最后一个元素
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { prev, v := collection.FindLoopedPrevInSlice([]int{1, 2, 3}, 1) fmt.Println(prev, v) }
Output: 0 1
func FindMaxFromComparableMap ¶
func FindMaxFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (result V)
FindMaxFromComparableMap 获取 map 中的最大值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindMaxFromComparableMap(map[int]int{1: 1, 2: 2, 3: 3}) fmt.Println(result) }
Output: 3
func FindMaxFromMap ¶
func FindMaxFromMap[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, handler OrderedValueGetter[V, N]) (result V)
FindMaxFromMap 获取 map 中的最大值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindMaxFromMap(map[int]int{1: 1, 2: 2, 3: 3}, func(v int) int { return v }) fmt.Println(result) }
Output: 3
func FindMaximumInComparableSlice ¶
FindMaximumInComparableSlice 获取切片中的最大值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindMaximumInComparableSlice([]int{1, 2, 3}) fmt.Println(result) }
Output: 3
func FindMaximumInSlice ¶
func FindMaximumInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (result V)
FindMaximumInSlice 获取切片中的最大值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindMaximumInSlice([]int{1, 2, 3}, func(v int) int { return v }) fmt.Println(result) }
Output: 3
func FindMin2MaxFromComparableMap ¶
func FindMin2MaxFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (min, max V)
FindMin2MaxFromComparableMap 获取 map 中的最小值和最大值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { minimum, maximum := collection.FindMin2MaxFromComparableMap(map[int]int{1: 1, 2: 2, 3: 3}) fmt.Println(minimum, maximum) }
Output: 1 3
func FindMin2MaxFromMap ¶
func FindMin2MaxFromMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (min, max V)
FindMin2MaxFromMap 获取 map 中的最小值和最大值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { minimum, maximum := collection.FindMin2MaxFromMap(map[int]int{1: 1, 2: 2, 3: 3}) fmt.Println(minimum, maximum) }
Output: 1 3
func FindMin2MaxInComparableSlice ¶
FindMin2MaxInComparableSlice 获取切片中的最小值和最大值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { minimum, maximum := collection.FindMin2MaxInComparableSlice([]int{1, 2, 3}) fmt.Println(minimum, maximum) }
Output: 1 3
func FindMin2MaxInSlice ¶
func FindMin2MaxInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (min, max V)
FindMin2MaxInSlice 获取切片中的最小值和最大值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { minimum, maximum := collection.FindMin2MaxInSlice([]int{1, 2, 3}, func(v int) int { return v }) fmt.Println(minimum, maximum) }
Output: 1 3
func FindMinFromComparableMap ¶
func FindMinFromComparableMap[M ~map[K]V, K comparable, V generic.Ordered](m M) (result V)
FindMinFromComparableMap 获取 map 中的最小值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindMinFromComparableMap(map[int]int{1: 1, 2: 2, 3: 3}) fmt.Println(result) }
Output: 1
func FindMinFromMap ¶
func FindMinFromMap[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, handler OrderedValueGetter[V, N]) (result V)
FindMinFromMap 获取 map 中的最小值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindMinFromMap(map[int]int{1: 1, 2: 2, 3: 3}, func(v int) int { return v }) fmt.Println(result) }
Output: 1
func FindMinimumInComparableSlice ¶
FindMinimumInComparableSlice 获取切片中的最小值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindMinimumInComparableSlice([]int{1, 2, 3}) fmt.Println(result) }
Output: 1
func FindMinimumInSlice ¶
func FindMinimumInSlice[S ~[]V, V any, N generic.Ordered](slice S, handler OrderedValueGetter[V, N]) (result V)
FindMinimumInSlice 获取切片中的最小值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindMinimumInSlice([]int{1, 2, 3}, func(v int) int { return v }) fmt.Println(result) }
Output: 1
func FindOrDefaultInComparableSlice ¶
func FindOrDefaultInComparableSlice[S ~[]V, V comparable](slice S, v V, defaultValue V) (t V)
FindOrDefaultInComparableSlice 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则返回默认值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindOrDefaultInComparableSlice([]int{1, 2, 3}, 2, 0) fmt.Println(result) }
Output: 2
func FindOrDefaultInSlice ¶
FindOrDefaultInSlice 判断切片中是否存在某个元素,返回第一个匹配的索引和元素,不存在则返回默认值
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.FindOrDefaultInSlice([]int{1, 2, 3}, 0, func(v int) bool { return v == 2 }) fmt.Println(result) }
Output: 2
func InAllComparableSlices ¶
func InAllComparableSlices[S ~[]V, V comparable](slices []S, v V) bool
InAllComparableSlices 检查 v 是否被包含在 slices 的每一项元素中
- 当 v 被包含在 slices 的每一项元素中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.InAllComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, 2) fmt.Println(result) }
Output: false
func InAllSlices ¶
func InAllSlices[S ~[]V, V any](slices []S, v V, handler ComparisonHandler[V]) bool
InAllSlices 检查 v 是否被包含在 slices 的每一项元素中,当 handler 返回 true 时,表示 v 和 slices 中的某个元素相匹配
- 当 v 被包含在 slices 的每一项元素中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.InAllSlices([][]int{{1, 2, 3}, {4, 5, 6}}, 2, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: false
func InComparableSlice ¶
func InComparableSlice[S ~[]V, V comparable](slice S, v V) bool
InComparableSlice 检查 v 是否被包含在 slice 中
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.InComparableSlice([]int{1, 2, 3}, 2) fmt.Println(result) }
Output: true
func InComparableSlices ¶
func InComparableSlices[S ~[]V, V comparable](slices []S, v V) bool
InComparableSlices 通过将多个切片合并后检查 v 是否被包含在 slices 中
- 当传入的 v 被包含在 slices 的任一成员中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.InComparableSlices([][]int{{1, 2, 3}, {4, 5, 6}}, 2) fmt.Println(result) }
Output: true
func InSlice ¶
func InSlice[S ~[]V, V any](slice S, v V, handler ComparisonHandler[V]) bool
InSlice 检查 v 是否被包含在 slice 中,当 handler 返回 true 时,表示 v 和 slice 中的某个元素相匹配
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.InSlice([]int{1, 2, 3}, 2, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func InSlices ¶
func InSlices[S ~[]V, V any](slices []S, v V, handler ComparisonHandler[V]) bool
InSlices 通过将多个切片合并后检查 v 是否被包含在 slices 中,当 handler 返回 true 时,表示 v 和 slices 中的某个元素相匹配
- 当传入的 v 被包含在 slices 的任一成员中时,返回 true
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.InSlices([][]int{{1, 2, 3}, {4, 5, 6}}, 2, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
func InvertMap ¶
func InvertMap[M ~map[K]V, N map[V]K, K, V comparable](m M) N
InvertMap 将映射的键和值互换
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.InvertMap(map[int]string{1: "a", 2: "b", 3: "c"}) fmt.Println(collection.AllKeyInMap(result, "a", "b", "c")) }
Output: true
func KeyInAllMaps ¶
func KeyInAllMaps[M ~map[K]V, K comparable, V any](maps []M, key K) bool
KeyInAllMaps 检查 key 是否被包含在 maps 的每一个元素中
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.KeyInAllMaps([]map[string]int{{"a": 1, "b": 2}, {"a": 1, "b": 2}}, "a") fmt.Println(result) }
Output: true
func KeyInMap ¶
func KeyInMap[M ~map[K]V, K comparable, V any](m M, key K) bool
KeyInMap 检查 m 中是否包含特定 key
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.KeyInMap(map[string]int{"a": 1, "b": 2}, "a") fmt.Println(result) }
Output: true
func LoopMap ¶
func LoopMap[M ~map[K]V, K comparable, V any](m M, f func(i int, key K, val V) bool)
LoopMap 迭代 m 中的每一个函数,并将键和值传递给 f 函数
- m 的迭代顺序是不确定的,因此每次迭代的顺序可能不同
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var result []int collection.LoopMap(map[string]int{"a": 1, "b": 2, "c": 3}, func(i int, key string, val int) bool { result = append(result, val) return true }) fmt.Println(collection.AllInComparableSlice(result, []int{1, 2, 3})) }
Output: true
func LoopMapByKeyGetterAsc ¶
func LoopMapByKeyGetterAsc[M ~map[K]V, K comparable, V comparable, N generic.Ordered](m M, getter func(k K) N, f func(i int, key K, val V) bool)
LoopMapByKeyGetterAsc 按照键的升序迭代 m 中的每一个函数,并将键和值传递给 f 函数
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result []int collection.LoopMapByKeyGetterAsc( m, func(k string) int { return m[k] }, func(i int, key string, val int) bool { result = append(result, val) return true }, ) fmt.Println(collection.AllInComparableSlice(result, []int{1, 2, 3})) }
Output: true
func LoopMapByKeyGetterDesc ¶
func LoopMapByKeyGetterDesc[M ~map[K]V, K comparable, V comparable, N generic.Ordered](m M, getter func(k K) N, f func(i int, key K, val V) bool)
LoopMapByKeyGetterDesc 按照键的降序迭代 m 中的每一个函数,并将键和值传递给 f 函数
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result []int collection.LoopMapByKeyGetterDesc( m, func(k string) int { return m[k] }, func(i int, key string, val int) bool { result = append(result, val) return true }, ) fmt.Println(collection.AllInComparableSlice(result, []int{3, 2, 1})) }
Output: true
func LoopMapByOrderedKeyAsc ¶
func LoopMapByOrderedKeyAsc[M ~map[K]V, K generic.Ordered, V any](m M, f func(i int, key K, val V) bool)
LoopMapByOrderedKeyAsc 按照键的升序迭代 m 中的每一个函数,并将键和值传递给 f 函数
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var result []int collection.LoopMapByOrderedKeyAsc(map[string]int{"a": 1, "b": 2, "c": 3}, func(i int, key string, val int) bool { result = append(result, val) return true }) fmt.Println(collection.AllInComparableSlice(result, []int{1, 2, 3})) }
Output: true
func LoopMapByOrderedKeyDesc ¶
func LoopMapByOrderedKeyDesc[M ~map[K]V, K generic.Ordered, V any](m M, f func(i int, key K, val V) bool)
LoopMapByOrderedKeyDesc 按照键的降序迭代 m 中的每一个函数,并将键和值传递给 f 函数
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var result []int collection.LoopMapByOrderedKeyDesc(map[string]int{"a": 1, "b": 2, "c": 3}, func(i int, key string, val int) bool { result = append(result, val) return true }) fmt.Println(collection.AllInComparableSlice(result, []int{3, 2, 1})) }
Output: true
func LoopMapByOrderedValueAsc ¶
func LoopMapByOrderedValueAsc[M ~map[K]V, K comparable, V generic.Ordered](m M, f func(i int, key K, val V) bool)
LoopMapByOrderedValueAsc 按照值的升序迭代 m 中的每一个函数,并将键和值传递给 f 函数
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var result []int collection.LoopMapByOrderedValueAsc(map[string]int{"a": 1, "b": 2, "c": 3}, func(i int, key string, val int) bool { result = append(result, val) return true }) fmt.Println(collection.AllInComparableSlice(result, []int{1, 2, 3})) }
Output: true
func LoopMapByOrderedValueDesc ¶
func LoopMapByOrderedValueDesc[M ~map[K]V, K comparable, V generic.Ordered](m M, f func(i int, key K, val V) bool)
LoopMapByOrderedValueDesc 按照值的降序迭代 m 中的每一个函数,并将键和值传递给 f 函数
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var result []int collection.LoopMapByOrderedValueDesc(map[string]int{"a": 1, "b": 2, "c": 3}, func(i int, key string, val int) bool { result = append(result, val) return true }) fmt.Println(collection.AllInComparableSlice(result, []int{3, 2, 1})) }
Output: true
func LoopMapByValueGetterAsc ¶
func LoopMapByValueGetterAsc[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, getter func(v V) N, f func(i int, key K, val V) bool)
LoopMapByValueGetterAsc 按照值的升序迭代 m 中的每一个函数,并将键和值传递给 f 函数
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result []int collection.LoopMapByValueGetterAsc( m, func(v int) int { return v }, func(i int, key string, val int) bool { result = append(result, val) return true }, ) fmt.Println(collection.AllInComparableSlice(result, []int{1, 2, 3})) }
Output: true
func LoopMapByValueGetterDesc ¶
func LoopMapByValueGetterDesc[M ~map[K]V, K comparable, V any, N generic.Ordered](m M, getter func(v V) N, f func(i int, key K, val V) bool)
LoopMapByValueGetterDesc 按照值的降序迭代 m 中的每一个函数,并将键和值传递给 f 函数
- 该函数会在 f 中传入一个从 0 开始的索引,用于表示当前迭代的次数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var m = map[string]int{"a": 1, "b": 2, "c": 3} var result []int collection.LoopMapByValueGetterDesc( m, func(v int) int { return v }, func(i int, key string, val int) bool { result = append(result, val) return true }, ) fmt.Println(collection.AllInComparableSlice(result, []int{3, 2, 1})) }
Output: true
func LoopSlice ¶
LoopSlice 迭代切片 slice 中的每一个函数,并将索引和值传递给 f 函数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var result []int collection.LoopSlice([]int{1, 2, 3, 4, 5}, func(i int, val int) bool { result = append(result, val) if uint(i) == 1 { return false } return true }) fmt.Println(result) }
Output: [1 2]
func MappingFromMap ¶
func MappingFromMap[M ~map[K]V, NM map[K]N, K comparable, V, N any](m M, handler func(value V) N) NM
MappingFromMap 将 map 中的元素进行转换
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.MappingFromMap(map[int]int{1: 1, 2: 2, 3: 3}, func(value int) int { return value + 1 }) fmt.Println(result) }
Output: map[1:2 2:3 3:4]
func MappingFromSlice ¶
func MappingFromSlice[S ~[]V, NS []N, V, N any](slice S, handler func(value V) N) NS
MappingFromSlice 将切片中的元素进行转换
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.MappingFromSlice([]int{1, 2, 3}, func(value int) int { return value + 1 }) fmt.Println(result) }
Output: [2 3 4]
func MergeMaps ¶
func MergeMaps[M ~map[K]V, K comparable, V any](maps ...M) (result M)
MergeMaps 合并 map,当多个 map 中存在相同的 key 时,后面的 map 中的 key 将会覆盖前面的 map 中的 key
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { m := collection.MergeMaps( map[int]int{1: 1, 2: 2, 3: 3}, map[int]int{4: 4, 5: 5, 6: 6}, ) fmt.Println(len(m)) }
Output: 6
func MergeMapsWithSkip ¶
func MergeMapsWithSkip[M ~map[K]V, K comparable, V any](maps ...M) (result M)
MergeMapsWithSkip 合并 map,当多个 map 中存在相同的 key 时,后面的 map 中的 key 将会被跳过
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { m := collection.MergeMapsWithSkip( map[int]int{1: 1}, map[int]int{1: 2}, ) fmt.Println(m[1]) }
Output: 1
func MergeSlice ¶
func MergeSlice[V any](values ...V) (result []V)
MergeSlice 合并切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { fmt.Println(collection.MergeSlice(1, 2, 3)) }
Output: [1 2 3]
func MergeSlices ¶
func MergeSlices[S ~[]V, V any](slices ...S) (result S)
MergeSlices 合并切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { fmt.Println( collection.MergeSlices( []int{1, 2, 3}, []int{4, 5, 6}, ), ) }
Output: [1 2 3 4 5 6]
func ReverseLoopSlice ¶
ReverseLoopSlice 逆序迭代切片 slice 中的每一个函数,并将索引和值传递给 f 函数
- 迭代过程将在 f 函数返回 false 时中断
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var result []int collection.ReverseLoopSlice([]int{1, 2, 3, 4, 5}, func(i int, val int) bool { result = append(result, val) if uint(i) == 1 { return false } return true }) fmt.Println(result) }
Output: [5 4 3 2]
func ReverseSlice ¶
func ReverseSlice[S ~[]V, V any](s *S)
ReverseSlice 将切片反转
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var s = []int{1, 2, 3} collection.ReverseSlice(&s) fmt.Println(s) }
Output: [3 2 1]
func Shuffle ¶
func Shuffle[S ~[]V, V any](slice *S)
Shuffle 对切片进行随机排序
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3} collection.Shuffle(&slice) fmt.Println(len(slice)) }
Output: 3
func ShuffleByClone ¶
func ShuffleByClone[S ~[]V, V any](slice S) S
ShuffleByClone 对切片进行随机排序,返回排序后的切片
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var slice = []int{1, 2, 3} result := collection.ShuffleByClone(slice) fmt.Println(len(result)) }
Output: 3
func SwapSlice ¶
SwapSlice 将切片中的两个元素进行交换
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { var s = []int{1, 2, 3} collection.SwapSlice(&s, 0, 1) fmt.Println(s) }
Output: [2 1 3]
func ValueInMap ¶
func ValueInMap[M ~map[K]V, K comparable, V any](m M, value V, handler ComparisonHandler[V]) bool
ValueInMap 检查 m 中是否包含特定 value,当 handler 返回 true 时,表示 value 和 m 中的某个元素相匹配
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/collection" ) func main() { result := collection.ValueInMap(map[string]int{"a": 1, "b": 2}, 2, func(source, target int) bool { return source == target }) fmt.Println(result) }
Output: true
Types ¶
type ComparisonHandler ¶
ComparisonHandler 用于比较 `source` 和 `target` 两个值是否相同的比较函数
- 该函数接受两个参数,分别是源值和目标值,返回 true 的情况下即表示两者相同
type OrderedValueGetter ¶
OrderedValueGetter 用于获取 v 的可排序字段值的函数