merger: Pass reports via closure, instead of by reference in args

This is an alternate way of solving the same problem as 4007a902a264e5ff2c3be6b269ade515c9c1c145,
but in a nicer way. Compared to using pointers, this approach more obviously preserves the original
behaviour, and is arguably more readable than the original code.

Credit to @rade for this approach.
This commit is contained in:
Mike Lang
2016-11-26 21:13:30 -08:00
parent 8f2e3e7d9b
commit d20381d30b

View File

@@ -48,15 +48,15 @@ func (smartMerger) Merge(reports []report.Report) report.Report {
case 1:
return reports[0]
}
c := make(chan *report.Report, l)
for i := range reports {
c <- &reports[i]
c := make(chan report.Report, l)
for _, r := range reports {
c <- r
}
for ; l > 1; l-- {
go func(left, right *report.Report) {
r := left.Merge(*right)
c <- &r
}(<-c, <-c)
left, right := <-c, <-c
go func() {
c <- left.Merge(right)
}()
}
return *<-c
return <-c
}