1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
list.go 933 Bytes
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-04-21 21:34 . feat: some features for lang
package list
import (
"reflect"
)
type List struct {
inner interface{}
v reflect.Value
}
func NewList(slice interface{}) *List {
return &List{
inner: slice,
v: reflect.ValueOf(slice),
}
}
func (r *List) value() reflect.Value {
return r.v
}
func (r *List) ForEach(accept func(i int)) {
for i := 0; i < r.Len(); i++ {
accept(i)
}
}
func (r *List) ForEachWithStop(accept func(i int) bool) {
for i := 0; i < r.Len(); i++ {
if accept(i) {
break
}
}
}
func (r *List) FindBy(accept func(i int) bool) (j int, ok bool) {
r.ForEachWithStop(func(i int) bool {
j = i
ok = accept(i)
return ok
})
if !ok {
j = -1
}
return
}
func (r *List) Find(o interface{}) (int, bool) {
return r.FindBy(func(i int) bool {
return r.value().Index(i).Interface() == o
})
}
func (r *List) Contains(o interface{}) (ok bool) {
_, ok = r.Find(o)
return
}
func (r *List) Len() int {
return r.value().Len()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.3.8

搜索帮助