From b2065d2c46c64a01ac8bad3b3073484810409a18 Mon Sep 17 00:00:00 2001 From: zetashift Date: Tue, 31 Oct 2023 22:50:27 +0100 Subject: [PATCH 1/2] Remove redundant drain from CSV writing example --- docs/examples.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/examples.md b/docs/examples.md index 1cbcab0..8f755ae 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -447,7 +447,6 @@ def writeCaseClassToCsv[A]( _.through(encodeUsingFirstHeaders(fullRows = true)) .through(fs2.text.utf8.encode) .through(Files[IO].writeAll(path)) - .drain /** Let's imagine we have a `Book` case class we would like to write to * a .csv file. @@ -499,7 +498,6 @@ object Helpers { _.through(encodeUsingFirstHeaders(fullRows = true)) .through(fs2.text.utf8.encode) .through(Files[IO].writeAll(path)) - .drain } From e173a24fdf116e69ee1e43b07b7425aa36c74e19 Mon Sep 17 00:00:00 2001 From: zetashift Date: Tue, 31 Oct 2023 23:15:11 +0100 Subject: [PATCH 2/2] Fixup code explanation for CSV writing. --- docs/examples.md | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/docs/examples.md b/docs/examples.md index 8f755ae..90a4c5e 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -422,7 +422,23 @@ object PerftConverter extends IOApp.Simple { ## Writing data to a CSV file -If you want to save a list of a case class into a CSV file this helper method may aid you: +If you want to save a list of a case class into a CSV file this utility may aid you: + +```scala +// Define your case class and derive an encoder for it +case class YourCaseClass(n: String, i: Int) +given CsvRowEncoder[YourCaseClass, String] = deriveCsvRowEncoder + +// Writes a case class as a csv given a path. +def writeCaseClassToCsv[A]( + path: Path +)(using CsvRowEncoder[A, String]): Pipe[IO, A, Nothing] = + _.through(encodeUsingFirstHeaders(fullRows = true)) + .through(fs2.text.utf8.encode) + .through(Files[IO].writeAll(path)) +``` + +As an example, let's imagine we have a `Book` class we would like to write to a `.csv` file. @:select(scala-version) @@ -436,11 +452,6 @@ import fs2.io.file.{Files, Path} import cats.effect.{IO, IOApp} import fs2.{Pipe, Stream} -/** Define your case class and derive an encoder for it */ -case class YourCaseClass(n: String, i: Int) -given CsvRowEncoder[YourCaseClass, String] = deriveCsvRowEncoder - -/** This is our helper function that writes a case class as a csv given a path. */ def writeCaseClassToCsv[A]( path: Path )(using CsvRowEncoder[A, String]): Pipe[IO, A, Nothing] = @@ -448,9 +459,7 @@ def writeCaseClassToCsv[A]( .through(fs2.text.utf8.encode) .through(Files[IO].writeAll(path)) -/** Let's imagine we have a `Book` case class we would like to write to - * a .csv file. - */ + object WriteBooksToCsv extends IOApp.Simple: case class Book(id: Long, name: String, isbn: String) given CsvRowEncoder[Book, String] = deriveCsvRowEncoder @@ -480,30 +489,15 @@ import fs2.io.file.{Files, Path} import cats.effect.{IO, IOApp} import fs2.{Pipe, Stream} -/** Define your case class and derive an encoder for it */ -case class YourCaseClass(n: String, i: Int) -object YourCaseClass { - implicit val csvRowEncoder: CsvRowEncoder[YourCaseClass, String] = - deriveCsvRowEncoder -} - object Helpers { - - /** This is our helper function that writes a case class as a csv given a - * path. - */ def writeCaseClassToCsv[A]( path: Path )(implicit encoder: CsvRowEncoder[A, String]): Pipe[IO, A, Nothing] = _.through(encodeUsingFirstHeaders(fullRows = true)) .through(fs2.text.utf8.encode) .through(Files[IO].writeAll(path)) - } -/** Let's imagine we have a `Book` case class we would like to write to a .csv - * file. - */ object WriteBooksToCsv extends IOApp.Simple { case class Book(id: Long, name: String, isbn: String) implicit val csvRowEncoder: CsvRowEncoder[Book, String] = deriveCsvRowEncoder