1 Star 2 Fork 3

kristas/booting-go

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
list.go 933 Bytes
Copy Edit Raw Blame History
kristas authored 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.4

Search