Getting sidetracked writing utility functions
This commit is contained in:
parent
8ca6f624d4
commit
869142a314
26
algs4/src/meatballhat.com/algs4/io.go
Normal file
26
algs4/src/meatballhat.com/algs4/io.go
Normal file
@ -0,0 +1,26 @@
|
||||
package algs4
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ReadInts(infile io.Reader) ([]int64, error) {
|
||||
ints := make([]int64, 0)
|
||||
|
||||
var i int64
|
||||
|
||||
fileReader := bufio.NewReader(infile)
|
||||
line, err := fileReader.ReadString('\n')
|
||||
for err == nil {
|
||||
i, err = strconv.ParseInt(line, 10, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ints = append(ints, i)
|
||||
line, err = fileReader.ReadString('\n')
|
||||
}
|
||||
|
||||
return ints, nil
|
||||
}
|
11
algs4/src/meatballhat.com/algs4/io_test.go
Normal file
11
algs4/src/meatballhat.com/algs4/io_test.go
Normal file
@ -0,0 +1,11 @@
|
||||
package algs4_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadInts(t *testing.T) {
|
||||
if 1 == 0 {
|
||||
t.Error("WAT")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user