functional-streams-for-scala/fs2

Name: fs2

Owner: functional-streams-for-scala

Description: Compositional, streaming I/O library for Scala

Created: 2013-03-06 17:34:15.0

Updated: 2018-01-16 16:48:58.0

Pushed: 2018-01-11 09:31:40.0

Homepage:

Size: 6266

Language: Scala

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

FS2: Functional Streams for Scala (previously 'Scalaz-Stream')

Build Status Gitter Chat Latest version

Quick links:

About the library

FS2 is a streaming I/O library. The design goals are compositionality, expressiveness, resource safety, and speed. Here's a simple example of its use:

rt cats.effect.{IO, Sync}
rt fs2.{io, text}
rt java.nio.file.Paths

fahrenheitToCelsius(f: Double): Double =
 - 32.0) * (5.0/9.0)

converter[F[_]](implicit F: Sync[F]): F[Unit] =
.file.readAll[F](Paths.get("testdata/fahrenheit.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("testdata/celsius.txt")))
.compile.drain

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

This will construct a F[Unit], converter, which reads lines incrementally from testdata/fahrenheit.txt, skipping blank lines and commented lines. It then parses temperatures in degrees Fahrenheit, converts these to Celsius, UTF-8 encodes the output, and writes incrementally to testdata/celsius.txt, using constant memory. The input and output files will be closed upon normal termination or if exceptions occur.

At the end it's saying that the effect F will be of type cats.effect.IO and then it's possible to invoke unsafeRunSync(). You can choose a different effect type or your own as long as it implements cats.effect.Sync.

The library supports a number of other interesting use cases:

Documentation and getting help

Blog posts and other external resources are listed on the Additional Resources page.

Where to get the latest version

The 0.10 release is almost complete and will be released when Cats 1.0 is released. Milestone builds are available now. The 0.10 migration guide summarizes the differences between 0.10 and 0.9. To get 0.10, add the following to your SBT build:

vailable for Scala 2.11, 2.12
aryDependencies += "co.fs2" %% "fs2-core" % "0.10.0-RC1" // For cats 1.0.1 and cats-effect 0.8

ptional I/O library
aryDependencies += "co.fs2" %% "fs2-io" % "0.10.0-RC1"

The 0.9 release is out and we recommend upgrading. You may want to first read the 0.9 migration guide if you are upgrading from 0.8 or earlier. To get 0.9, add the following to your SBT build:

vailable for Scala 2.11, 2.12
aryDependencies += "co.fs2" %% "fs2-core" % "0.9.7"

ptional I/O library
aryDependencies += "co.fs2" %% "fs2-io" % "0.9.7"

The fs2-core library is also supported on Scala.js:

vailable for Scala 2.11.8, 2.12.0
aryDependencies += "co.fs2" %%% "fs2-core" % "0.9.7"

API docs:

The previous stable release is 0.8.4 (source).

Projects using FS2

If you have a project you'd like to include in this list, either open a PR or let us know in the gitter channel and we'll add a link to it here.

Related projects

FS2 has evolved from earlier work on streaming APIs in Scala and Haskell and in Scala. Some influences:

Presentations, Blogs, etc.

See Additional resources.

Acknowledgments

YourKit

Special thanks to YourKit for supporting this project's ongoing performance tuning efforts with licenses to their excellent product.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.