Skip to content

Instantly share code, notes, and snippets.

@lbialy
Last active December 29, 2023 21:09
Show Gist options
  • Save lbialy/4fb409fdf5aef4f08527bea44835a4f2 to your computer and use it in GitHub Desktop.
Save lbialy/4fb409fdf5aef4f08527bea44835a4f2 to your computer and use it in GitHub Desktop.
comparing word counting: std vs fs2
//> using scala "3.3.1"
//> using lib "co.fs2::fs2-io:3.9.3"
import scala.util.*
import scala.io.Source
def safePrintln(s: Any): Either[Throwable, Unit] =
try Right(println(s))
catch case err: Throwable => Left(err)
def countWordsStd(fileName: String): Either[Throwable, Unit] =
Using(Source.fromFile(fileName)) { source =>
source.getLines
.flatMap(_.split("\\s"))
.map(_ => 1)
.sum
}.toEither.flatMap(safePrintln(_))
import fs2.*, Stream.*
import cats.effect.IO
import fs2.io.file.{Files, Path}
def stdout[A]: Pipe[IO, A, Unit] =
_.evalMap(a => IO(println(a)))
// in the talk:
// io.linesR(fileName)
// .flatMap(s => emits(s.split("\\s")))
// .map(_ => 1)
// .fold(0)(_ + _)
// .to(stdout)
def countWordsFs2(fileName: String): IO[Unit] =
Files[IO]
.readUtf8Lines(Path(fileName))
.flatMap(s => emits(s.split("\\s")))
.map(_ => 1)
.fold(0)(_ + _)
.through(stdout)
.compile
.drain
import cats.effect.unsafe.implicits.global
@main def main =
println("Using fs2:")
countWordsFs2("lorem").unsafeRunSync()
println("Using scala stdlib:")
countWordsStd("lorem") match
case Left(err) => println(err)
case Right(_) => println("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment