9

I'm want to know how to convert Scala fs2 Stream to string, from fs2 github readme example:

def converter[F[_]](implicit F: Sync[F]): F[Unit] = {
  val path = "/Users/lorancechen/version_control_project/_unlimited-works/git-server/src/test/resources"

  io.file.readAll[F](Paths.get(s"$path/fs.txt"), 4096)
    .through(text.utf8Decode)
    .through(text.lines)
    .filter(s => !s.trim.isEmpty && !s.startsWith("//"))
    .map(line => fahrenheitToCelsius(line.toDouble).toString)
    .intersperse("\n")
    .through(text.utf8Encode)
    .through(io.file.writeAll(Paths.get(s"$path/fs-output.txt")))
    .compile.drain

}

// at the end of the universe...
val u: Unit = converter[IO].unsafeRunSync()

How to get result to String rather then to another file?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
LoranceChen
  • 2,453
  • 2
  • 22
  • 48

2 Answers2

7

If you have a Stream[F, String], you can call .compile.string to convert your stream to a F[String].

val s: Stream[IO, String] = ???
val io: IO[String] = s.compile.string
val str: String = io.unsafeRunSync()
Jasper-M
  • 14,966
  • 2
  • 26
  • 37
3

If you want get all String elements running in your stream, you can use runFold to materialize it. A simplistic example:

def converter[F[_]](implicit F: Sync[F]): F[List[String]] = {
  val path = "/Users/lorancechen/version_control_project/_unlimited-works/git-server/src/test/resources"

  io.file.readAll[F](Paths.get(s"$path/fs.txt"), 4096)
    .through(text.utf8Decode)
    .through(text.lines)
    .filter(s => !s.trim.isEmpty && !s.startsWith("//"))
    .runFold(List.empty[String]) { case (acc, str) => str :: acc }
}

And then:

val list: List[String] = converter[IO].unsafeRunSync()
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • 1
    thanks, it works well. Besides, latest fs2(v0.10.0-M11) recommend use `compile.fold` instand. – LoranceChen Jan 25 '18 at 14:15
  • This is not the appropriate solution on more recent versions. `.compile.to(List)` to aggregate each element into a list or `.compile.string` to make directly into a singular string. Like the more recent answer below. – ChristopherDavenport Apr 28 '21 at 15:29