github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/partition.go (about) 1 package isc 2 3 func Partition[T any](list []T, partition int) [][]T { 4 return PartitionWithCal(list, func(int) int { 5 return partition 6 }) 7 } 8 9 // PartitionWithCal 计算partition数 10 // f: 入参为数组长度,返回partition数 11 func PartitionWithCal[T any](list []T, f func(int) int) [][]T { 12 var array [][]T 13 14 length := len(list) 15 if length == 0 { 16 return array 17 } 18 19 partiton := f(length) 20 if partiton <= 0 { 21 array = append(array, list) 22 return array 23 } 24 //list下标 25 n := 0 26 partitonLen := length / partiton 27 for i := 0; i < partiton; i++ { 28 var subList []T 29 for j := 0; j < partitonLen; j++ { 30 subList = append(subList, list[n]) 31 n++ 32 } 33 //list有多余 34 if i == partiton-1 { 35 for k := n; k < length; k++ { 36 subList = append(subList, list[k]) 37 } 38 } 39 array = append(array, subList) 40 } 41 return array 42 }