-
Notifications
You must be signed in to change notification settings - Fork 9
Update examples.md #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0168c5c
Update examples.md
Quafadas 6f2e7b2
Merge branch 'typelevel:main' into main
Quafadas 6b75cc1
Improve CSV examples as per feedback
Quafadas c4a4a17
Update docs/examples.md
Quafadas 508ac9b
Update docs/examples.md
Quafadas 712c167
Update docs/examples.md
Quafadas d00af42
Update docs/examples.md
Quafadas e9e95d5
Add runnable example for CSV writing example.
zetashift e4e4847
Merge pull request #1 from zetashift/runnable-example
Quafadas be34afb
Fix CSV writing example
zetashift b20a53c
Merge pull request #2 from zetashift/fix-csv-example
Quafadas b2065d2
Remove redundant drain from CSV writing example
zetashift e173a24
Fixup code explanation for CSV writing.
zetashift ca1dd64
Merge pull request #3 from zetashift/fix-csv-example
Quafadas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,7 +166,7 @@ object Main extends CommandIOApp("mkString", "Concatenates strings from stdin") | |
|
|
||
| ## Parsing and transforming a CSV file | ||
|
|
||
| Here, [fs2-data-csv] is used to read and parse a comma separated file. | ||
| Here, [fs2-data-csv] is used to read and parse a comma separated file. | ||
| Manual encoders and decoders are defined for our `Passenger`s to show you how to do everything from scratch. | ||
|
|
||
| Let's start with a CSV file that has records of fictious passengers registered for a flight: | ||
|
|
@@ -233,7 +233,7 @@ val input = Files[IO] | |
|
|
||
| object CSVPrinter extends IOApp.Simple: | ||
|
|
||
| /** First we'll do some logging for each row, | ||
| /** First we'll do some logging for each row, | ||
| * and then calculate and print the mean age */ | ||
| val run = | ||
| input | ||
|
|
@@ -310,7 +310,7 @@ object CSVPrinter extends IOApp.Simple { | |
| .through(decodeUsingHeaders[Passenger]()) | ||
|
|
||
|
|
||
| /** First we'll do some logging for each row, | ||
| /** First we'll do some logging for each row, | ||
| * and then calculate and print the mean age */ | ||
| val run = | ||
| input | ||
|
|
@@ -420,6 +420,107 @@ 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 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)) | ||
| ``` | ||
|
Comment on lines
+427
to
+439
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's scala 3 only, can you add the scala 2 tab too? |
||
|
|
||
| As an example, let's imagine we have a `Book` class we would like to write to a `.csv` file. | ||
|
|
||
| @:select(scala-version) | ||
|
|
||
| @:choice(scala-3) | ||
| ```scala mdoc:reset:silent | ||
| //> using lib "org.typelevel::toolkit::@VERSION@" | ||
|
|
||
| import fs2.data.csv.* | ||
| import fs2.data.csv.generic.semiauto.* | ||
| import fs2.io.file.{Files, Path} | ||
| import cats.effect.{IO, IOApp} | ||
| import fs2.{Pipe, Stream} | ||
|
|
||
| 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)) | ||
|
|
||
|
|
||
| object WriteBooksToCsv extends IOApp.Simple: | ||
| case class Book(id: Long, name: String, isbn: String) | ||
| given CsvRowEncoder[Book, String] = deriveCsvRowEncoder | ||
|
|
||
| val input = Seq( | ||
| Book(1, "Programming in Scala", "9780997148008"), | ||
| Book(2, "Hands-on Scala Programming", "9798387677205"), | ||
| Book(3, "Functional Programming in Scala", "9781617299582") | ||
| ) | ||
|
|
||
| def run: IO[Unit] = | ||
| Stream | ||
| .emits(input) | ||
| .through(writeCaseClassToCsv(Path("books.csv"))) | ||
| .compile | ||
| .drain *> IO.println("Finished writing books to books.csv.") | ||
| ``` | ||
|
|
||
| @:choice(scala-2) | ||
|
|
||
| ```scala mdoc:reset:silent | ||
| //> using lib "org.typelevel::toolkit::@VERSION@" | ||
|
|
||
| import fs2.data.csv._ | ||
| import fs2.data.csv.generic.semiauto._ | ||
| import fs2.io.file.{Files, Path} | ||
| import cats.effect.{IO, IOApp} | ||
| import fs2.{Pipe, Stream} | ||
|
|
||
| object Helpers { | ||
| 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)) | ||
| } | ||
|
|
||
| object WriteBooksToCsv extends IOApp.Simple { | ||
| case class Book(id: Long, name: String, isbn: String) | ||
| implicit val csvRowEncoder: CsvRowEncoder[Book, String] = deriveCsvRowEncoder | ||
|
|
||
| val input = Seq( | ||
| Book(1, "Programming in Scala", "9780997148008"), | ||
| Book(2, "Hands-on Scala Programming", "9798387677205"), | ||
| Book(3, "Functional Programming in Scala", "9781617299582") | ||
| ) | ||
|
|
||
| def run: IO[Unit] = | ||
| Stream | ||
| .emits(input) | ||
| .through(Helpers.writeCaseClassToCsv(Path("books.csv"))) | ||
| .compile | ||
| .drain *> IO.println("Finished writing books to books.csv.") | ||
| } | ||
| ``` | ||
|
|
||
| @:@ | ||
|
|
||
|
|
||
|
|
||
| [fs2]: https://fs2.io/#/ | ||
| [fs2-data-csv]: https://fs2-data.gnieh.org/documentation/csv/ | ||
| [decline]: https://ben.kirw.in/decline/ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.