fs2-io


License

License

MIT
GroupId

GroupId

co.fs2
ArtifactId

ArtifactId

fs2-io_2.11
Last Version

Last Version

2.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

fs2-io
fs2-io
Project URL

Project URL

https://github.com/functional-streams-for-scala/fs2
Project Organization

Project Organization

co.fs2
Source Code Management

Source Code Management

https://github.com/functional-streams-for-scala/fs2

Download fs2-io_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/co.fs2/fs2-io_2.11/ -->
<dependency>
    <groupId>co.fs2</groupId>
    <artifactId>fs2-io_2.11</artifactId>
    <version>2.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/co.fs2/fs2-io_2.11/
implementation 'co.fs2:fs2-io_2.11:2.1.0'
// https://jarcasting.com/artifacts/co.fs2/fs2-io_2.11/
implementation ("co.fs2:fs2-io_2.11:2.1.0")
'co.fs2:fs2-io_2.11:jar:2.1.0'
<dependency org="co.fs2" name="fs2-io_2.11" rev="2.1.0">
  <artifact name="fs2-io_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='co.fs2', module='fs2-io_2.11', version='2.1.0')
)
libraryDependencies += "co.fs2" % "fs2-io_2.11" % "2.1.0"
[co.fs2/fs2-io_2.11 "2.1.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
co.fs2 : fs2-core_2.11 jar 2.1.0
org.typelevel : cats-core_2.11 jar 2.0.0
org.typelevel : cats-effect_2.11 jar 2.0.0

Project Modules

There are no modules declared in this project.

FS2: Functional Streams for Scala

Continuous Integration Gitter Chat Maven Central

Overview

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).

FS2 is available for Scala 2.12, Scala 2.13, and Scala.js. FS2 is built upon two major functional libraries for Scala, Cats, and Cats-Effect. Regardless of those dependencies, FS2 core types (streams and pulls) are polymorphic in the effect type (as long as it is compatible with cats-effect typeclasses), and thus FS2 can be used with other effect libraries, such as Monix.

Prior to the 0.9 release in 2016, FS2 was known as scalaz-stream, which was based on the scalaz library.

Getting Started

Quick links:

Where to get the latest version

The latest version is 2.4.x. See the badge at the top of the README for the exact version number.

If upgrading from the 1.0 series, see the release notes for 2.0.0 for help with upgrading.

// available for 2.12, 2.13
libraryDependencies += "co.fs2" %% "fs2-core" % "<version>" // For cats 2 and cats-effect 2

// optional I/O library
libraryDependencies += "co.fs2" %% "fs2-io" % "<version>"

// optional reactive streams interop
libraryDependencies += "co.fs2" %% "fs2-reactive-streams" % "<version>"

// optional experimental library
libraryDependencies += "co.fs2" %% "fs2-experimental" % "<version>"

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

libraryDependencies += "co.fs2" %%% "fs2-core" % "<version>"

There are detailed migration guides for migrating from older versions.

Example

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:

import cats.effect.{Blocker, ExitCode, IO, IOApp, Resource}
import fs2.{io, text, Stream}
import java.nio.file.Paths

object Converter extends IOApp {

  val converter: Stream[IO, Unit] = Stream.resource(Blocker[IO]).flatMap { blocker =>
    def fahrenheitToCelsius(f: Double): Double =
      (f - 32.0) * (5.0/9.0)

    io.file.readAll[IO](Paths.get("testdata/fahrenheit.txt"), blocker, 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"), blocker))
  }

  def run(args: List[String]): IO[ExitCode] =
    converter.compile.drain.as(ExitCode.Success)
}

This will construct a program that 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.

Note that this example is specialised to IO for simplicity, but Stream is fully polymorphic in the effect type (the F[_] in Stream[F, A]), as long as F[_] is compatible with the cats-effect typeclasses.

The library supports a number of other interesting use cases:

  • Zipping and merging of streams: A streaming computation may read from multiple sources in a streaming fashion, zipping or merging their elements using an arbitrary function. In general, clients have a great deal of flexibility in what sort of topologies they can define, due to Stream being a first class entity with a very rich algebra of combinators.
  • Dynamic resource allocation: A streaming computation may allocate resources dynamically (for instance, reading a list of files to process from a stream built off a network socket), and the library will ensure these resources get released upon normal termination or if exceptions occur.
  • Nondeterministic and concurrent processing: A computation may read from multiple input streams simultaneously, using whichever result comes back first, and a pipeline of transformations can allow for nondeterminism and queueing at each stage. Due to several concurrency combinators and data structures, streams can be used as light-weight, declarative threads to build complex concurrent behaviour compositionally.

These features mean that FS2 goes beyond streaming I/O to offer a very general and declarative model for arbitrary control flow.

Documentation and getting help

  • There are Scaladoc API documentations for the core library, which defines and implements the core types for streams and pulls, as well as the type aliases for pipes and sinks. The io library provides FS2 bindings for NIO-based file I/O and TCP/UDP networking.
  • The official guide is a good starting point for learning more about the library.
  • The documentation page is intended to serve as a list of all references, including conference presentation recordings, academic papers, and blog posts, on the use and implementation of fs2.
  • The FAQ has frequently asked questions. Feel free to open issues or PRs with additions to the FAQ!
  • Also feel free to come discuss and ask/answer questions in the gitter channel and/or on StackOverflow using the tag FS2. Gitter will generally get you a quicker answer.

Projects using FS2

You can find a list of libraries and integrations with data stores built on top of FS2 here: https://fs2.io/ecosystem.html.

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.

Adopters

Here's a (non-exhaustive) list of companies that use FS2 in production. Don't see yours? You can add it in a PR!

Acknowledgments

YourKit

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

Code of Conduct

See the Code of Conduct.

co.fs2

Versions

Version
2.1.0
2.0.1
2.0.0
1.1.0-M2
1.1.0-M1
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-RC2
1.0.0-RC1
1.0.0-M5
1.0.0-M4
1.0.0-M3
1.0.0-M2
1.0.0-M1
0.10.7
0.10.6
0.10.5
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.10.0-RC2
0.10.0-RC1
0.10.0-M11
0.10.0-M10
0.10.0-M9
0.10.0-M8
0.10.0-M7
0.10.0-M6
0.10.0-M5
0.10.0-M4
0.10.0-M3
0.10.0-M2
0.10.0-M1
0.9.7
0.9.6
0.9.5
0.9.5-RC1
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.9.0-RC2
0.9.0-RC1
0.9.0-M6
0.9.0-M5
0.9.0-M4
0.9.0-M3
0.9.0-M2
0.9.0-M1