-
Notifications
You must be signed in to change notification settings - Fork 225
/
union.go
40 lines (34 loc) · 837 Bytes
/
union.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package linq
// Union produces the set union of two collections.
//
// This method excludes duplicates from the return set. This is different
// behavior to the Concat method, which returns all the elements in the input
// collection including duplicates.
func (q Query) Union(q2 Query) Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
next2 := q2.Iterate()
set := make(map[interface{}]bool)
use1 := true
return func() (item interface{}, ok bool) {
if use1 {
for item, ok = next(); ok; item, ok = next() {
if _, has := set[item]; !has {
set[item] = true
return
}
}
use1 = false
}
for item, ok = next2(); ok; item, ok = next2() {
if _, has := set[item]; !has {
set[item] = true
return
}
}
return
}
},
}
}