feat(Go-Tool):2020/11/12:新增linq包使用示例(未完成)

This commit is contained in:
Huangzj
2020-11-12 18:10:15 +08:00
parent 3616c4b5e5
commit 2ac2b2666f
33 changed files with 2517 additions and 0 deletions
+46
View File
@@ -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
}
},
}
}