union.go 837 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package help
  2. // Union produces the set union of two collections.
  3. //
  4. // This method excludes duplicates from the return set. This is different
  5. // behavior to the Concat method, which returns all the elements in the input
  6. // collection including duplicates.
  7. func (q Query) Union(q2 Query) Query {
  8. return Query{
  9. Iterate: func() Iterator {
  10. next := q.Iterate()
  11. next2 := q2.Iterate()
  12. set := make(map[interface{}]bool)
  13. use1 := true
  14. return func() (item interface{}, ok bool) {
  15. if use1 {
  16. for item, ok = next(); ok; item, ok = next() {
  17. if _, has := set[item]; !has {
  18. set[item] = true
  19. return
  20. }
  21. }
  22. use1 = false
  23. }
  24. for item, ok = next2(); ok; item, ok = next2() {
  25. if _, has := set[item]; !has {
  26. set[item] = true
  27. return
  28. }
  29. }
  30. return
  31. }
  32. },
  33. }
  34. }