Skip to content

Commit

Permalink
Reuse function
Browse files Browse the repository at this point in the history
  • Loading branch information
suzaku committed Sep 18, 2021
1 parent 5eeac06 commit 6c467f5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
8 changes: 3 additions & 5 deletions set/subtract.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ limitations under the License.
package set

import (
"bufio"
"io"
)

func Subtract(f1, f2 io.Reader) <-chan string {
ch := make(chan string, 16)
chLines1 := readNonEmptyLines(f1)
go func() {
defer close(ch)
scanner1 := bufio.NewScanner(f1)
searcher := &rowSearcher{
chRowsInBulk: readLinesInBulk(f2, 64),
}
var lastLine string
var f2Exhausted bool
for scanner1.Scan() {
line := scanner1.Text()
if len(line) == 0 || line == lastLine {
for line := range chLines1 {
if line == lastLine {
continue
}
lastLine = line
Expand Down
17 changes: 0 additions & 17 deletions set/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package set

import (
"bufio"
"io"
)

Expand Down Expand Up @@ -88,19 +87,3 @@ func Union(f1, f2 io.Reader) <-chan string {
}()
return ch
}

func readNonEmptyLines(r io.Reader) <-chan string {
scanner := bufio.NewScanner(r)
chLine := make(chan string, 10)
go func() {
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
continue
}
chLine <- line
}
close(chLine)
}()
return chLine
}
16 changes: 16 additions & 0 deletions set/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ func (rs *rowSearcher) Search(row string) (found bool, inRange bool, exhausted b
}
return
}

func readNonEmptyLines(r io.Reader) <-chan string {
scanner := bufio.NewScanner(r)
chLine := make(chan string, 10)
go func() {
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
continue
}
chLine <- line
}
close(chLine)
}()
return chLine
}

0 comments on commit 6c467f5

Please # to comment.