feat: stream.Slice 新增 Indexes 和 Map 函数

This commit is contained in:
kercylan98 2023-09-08 14:44:48 +08:00
parent d9b68fc037
commit 5024022366
1 changed files with 18 additions and 0 deletions

View File

@ -19,6 +19,24 @@ func (slf Slice[V]) Zoom(newSize int) Slice[V] {
return slice.Zoom(newSize, slf)
}
// Indexes 将切片转换为索引切片
func (slf Slice[V]) Indexes() Slice[int] {
var s = make([]int, len(slf))
for i := 0; i < len(s); i++ {
s[i] = i
}
return s
}
// Map 将切片转为 map
func (slf Slice[V]) Map() Map[int, V] {
var m = make(map[int]V, len(slf))
for k, v := range slf {
m[k] = v
}
return m
}
// Chunk 是 slice.Chunk 的快捷方式
func (slf Slice[V]) Chunk(size int) Slices[V] {
chunks := slice.Chunk(slf, size)