Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert scala fs2 stream to string?

Tags:

scala

fs2

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?

like image 388
LoranceChen Avatar asked Jan 25 '18 10:01

LoranceChen


People also ask

What is FS2 Scala?

FS2 is a library for purely functional, effectful, and polymorphic stream processing library in the Scala programming language. Its design goals are compositionality, expressiveness, resource safety, and speed. The name is a modified acronym for Functional Streams for Scala (FSS, or FS2).

What is a stream in Scala?

The Stream is a lazy lists where elements are evaluated only when they are needed. This is a scala feature. Scala supports lazy computation. It increases performance of our program. Streams have the same performance characteristics as lists.


2 Answers

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()
like image 189
Jasper-M Avatar answered Sep 30 '22 06:09

Jasper-M


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()
like image 30
Yuval Itzchakov Avatar answered Sep 30 '22 06:09

Yuval Itzchakov