Skip to content

Commit

Permalink
feat: slice 包新增 Zoom 函数, stream 包支持 Zoom 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Sep 8, 2023
1 parent d72f185 commit 62ef35a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions utils/slice/zoom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package slice

// Zoom 将切片的长度缩放到指定的大小,如果 newSize 小于 slice 的长度,则会截断 slice,如果 newSize 大于 slice 的长度,则会在 slice 的末尾添加零值数据
func Zoom[V any](newSize int, slice []V) []V {
if newSize < 0 {
newSize = 0
}
var s = make([]V, newSize)
copy(s, slice)
return s
}
12 changes: 11 additions & 1 deletion utils/stream/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ func (slf Slice[V]) Slice() []V {
return slf
}

// Chunk 的快捷方式
// Copy 复制一份切片
func (slf Slice[V]) Copy() Slice[V] {
return slice.Copy(slf)
}

// Zoom 是 slice.Zoom 的快捷方式
func (slf Slice[V]) Zoom(newSize int) Slice[V] {
return slice.Zoom(newSize, slf)
}

// Chunk 是 slice.Chunk 的快捷方式
func (slf Slice[V]) Chunk(size int) Slices[V] {
chunks := slice.Chunk(slf, size)
result := make(Slices[V], len(chunks))
Expand Down

0 comments on commit 62ef35a

Please sign in to comment.