-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuniq.go
More file actions
30 lines (29 loc) · 986 Bytes
/
uniq.go
File metadata and controls
30 lines (29 loc) · 986 Bytes
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
package extsort
// UniqStringChan returns a channel that filters out consecutive duplicate strings from the input.
// This function assumes the input channel provides strings in sorted order and uses string equality
// to detect duplicates. It preserves the first occurrence of each unique string while filtering
// out subsequent duplicates. The function is optimized for sorted inputs where duplicates appear
// consecutively, making it suitable for post-processing sorted results from external sort operations.
//
// The returned channel will be closed when the input channel is closed.
// This function spawns a goroutine that will terminate when the input channel is closed.
func UniqStringChan(in chan string) chan string {
out := make(chan string)
go func() {
var prior string
priorSet := false
for d := range in {
if priorSet {
if d == prior {
continue
}
} else {
priorSet = true
}
out <- d
prior = d
}
close(out)
}()
return out
}