summaryrefslogtreecommitdiff
path: root/aoc/inputters.go
diff options
context:
space:
mode:
Diffstat (limited to 'aoc/inputters.go')
-rw-r--r--aoc/inputters.go42
1 files changed, 24 insertions, 18 deletions
diff --git a/aoc/inputters.go b/aoc/inputters.go
index f677177..33e24b4 100644
--- a/aoc/inputters.go
+++ b/aoc/inputters.go
@@ -6,34 +6,40 @@ import (
"strconv"
)
-// Read all lines into type T
-type ReadLines[T any] func(lines []string) (T, error)
+type (
+ Reader[T any] func(ctx context.Context, scanner *bufio.Scanner) (T, error)
+ Lines[T any] func(lines []string) (T, error)
+ ByLine[T any] func(line string) (T, error)
+)
-func (l ReadLines[T]) Read(ctx context.Context, scanner *bufio.Scanner) (T, error) {
- lines := []string{}
- for scanner.Scan() {
- lines = append(lines, scanner.Text())
+// Read all lines into type T
+func ReadLines[T any](l Lines[T]) Reader[T] {
+ return func(ctx context.Context, scanner *bufio.Scanner) (T, error) {
+ lines := []string{}
+ for scanner.Scan() {
+ lines = append(lines, scanner.Text())
+ }
+ return l(lines)
}
- return l(lines)
}
// Read line by line into type T, collect in []T
-type ReadByLine[T any] func(line string) (T, error)
-
-func (l ReadByLine[T]) Read(ctx context.Context, scanner *bufio.Scanner) ([]T, error) {
- lines := []T{}
- for scanner.Scan() {
- if t, err := l(scanner.Text()); err == nil {
- lines = append(lines, t)
- } else {
- return nil, err
+func ReadByLine[T any](l ByLine[T]) Reader[[]T] {
+ return func(ctx context.Context, scanner *bufio.Scanner) ([]T, error) {
+ lines := []T{}
+ for scanner.Scan() {
+ if t, err := l(scanner.Text()); err == nil {
+ lines = append(lines, t)
+ } else {
+ return nil, err
+ }
}
+ return lines, nil
}
- return lines, nil
}
// Read lines into ints
-var ReadInts = ReadByLine[int](func(line string) (int, error) {
+var ReadInts = ReadByLine(func(line string) (int, error) {
if i, err := strconv.Atoi(line); err != nil {
return 0, err
} else {