Hey! I don’t see many ZIO related questions here, I hope the topic is still welcome. If not, I’ll be happily pointed somewhere else.
So I’m toying around with this year’s advent of code and ZIO, and I’d like to have the inputs as .txt files. That means I’ll have to read those contents from disk into my program, and I decided to go with List[String]
because after all, text is always string, and parsing can be done more specifically later on.
To this point, I’ve come this far:
// return type annotation is just for clarification, I'm not settled with having this exact type
private def parseFile(fileName: String): ZIO[Any, Throwable, Seq[String]] =
val path = "src/main/resources/" + fileName
val lines: zio.stream.Stream[Throwable, String] =
zio.stream.Stream.fromIteratorManaged(
ZManaged.fromAutoCloseable(
Task(Source.fromFile(path))
).map(_.getLines())
)
lines
.runCollect
val program =
for
fileContent <- parseFile("0101.txt")
_ <- putStrLn(s"$fileContent")
yield ()
def run(args: List[String]) = // main function
program.exitCode
The input file I’m working on (link will only work if you are logged in yourself) looks something like this:
143
147
...
187
...
This somewhat works, but the output looks like Chunk(143,147,...,187...)
, which means:
a) it’s a singular String
instead of List[String]
despite calling getLines()
and the annotated types
b) surrounded by Chunk(...)
which breaks further parsing down the line
I believe lines.runCollect
might not do what I think it does, as well as reading the file via a Stream
was the wrong approach in the first place. My search for how to read files functionally using ZIO has always yielded examples done in a similar fashion though.