feat(Go-Tool):2020/11/12:新增linq包使用示例(未完成)
This commit is contained in:
+46
@@ -0,0 +1,46 @@
|
||||
package linq
|
||||
|
||||
// Where filters a collection of values based on a predicate.
|
||||
func (q Query) Where(predicate func(interface{}) bool) Query {
|
||||
return Query{
|
||||
Iterate: func() Iterator {
|
||||
next := q.Iterate()
|
||||
|
||||
return func() (item interface{}, ok bool) {
|
||||
for item, ok = next(); ok; item, ok = next() {
|
||||
if predicate(item) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// WhereIndexed filters a collection of values based on a predicate.
|
||||
// Each element's index is used in the logic of the predicate function.
|
||||
//
|
||||
// The first argument represents the zero-based index of the element within collection.
|
||||
// The second argument of predicate represents the element to test.
|
||||
func (q Query) WhereIndexed(predicate func(int, interface{}) bool) Query {
|
||||
return Query{
|
||||
Iterate: func() Iterator {
|
||||
next := q.Iterate()
|
||||
index := 0
|
||||
|
||||
return func() (item interface{}, ok bool) {
|
||||
for item, ok = next(); ok; item, ok = next() {
|
||||
if predicate(index, item) {
|
||||
return
|
||||
}
|
||||
|
||||
index++
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user