feat(Go-Tool):2020/11/12:新增linq包使用示例(未完成)
This commit is contained in:
+59
@@ -0,0 +1,59 @@
|
||||
package linq
|
||||
|
||||
// Except produces the set difference of two sequences.
|
||||
// The set difference is the members of the first sequence
|
||||
// that don't appear in the second sequence.
|
||||
func (q Query) Except(q2 Query) Query {
|
||||
return Query{
|
||||
Iterate: func() Iterator {
|
||||
next := q.Iterate()
|
||||
|
||||
next2 := q2.Iterate()
|
||||
set := make(map[interface{}]bool)
|
||||
for i, ok := next2(); ok; i, ok = next2() {
|
||||
set[i] = true
|
||||
}
|
||||
|
||||
return func() (item interface{}, ok bool) {
|
||||
for item, ok = next(); ok; item, ok = next() {
|
||||
if _, has := set[item]; !has {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ExceptBy invokes a transform function on each element of a collection
|
||||
// and produces the set difference of two sequences.
|
||||
// The set difference is the members of the first sequence
|
||||
// that don't appear in the second sequence.
|
||||
func (q Query) ExceptBy(
|
||||
q2 Query, selector func(interface{}) interface{}) Query {
|
||||
return Query{
|
||||
Iterate: func() Iterator {
|
||||
next := q.Iterate()
|
||||
|
||||
next2 := q2.Iterate()
|
||||
set := make(map[interface{}]bool)
|
||||
for i, ok := next2(); ok; i, ok = next2() {
|
||||
s := selector(i)
|
||||
set[s] = true
|
||||
}
|
||||
|
||||
return func() (item interface{}, ok bool) {
|
||||
for item, ok = next(); ok; item, ok = next() {
|
||||
s := selector(item)
|
||||
if _, has := set[s]; !has {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user