Code Monkey home page Code Monkey logo

fs2's Introduction

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:

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

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

def converter[F[_]](implicit F: Sync[F]): F[Unit] =
  io.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

// at the end of the universe...
val 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:

  • 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 Pipe2. In general, clients have a great deal of flexibility in what sort of topologies they can define--source, sink, and effectful channels are all first-class concepts in the library.
  • 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.

Documentation and getting help

  • The official guide is a good starting point for learning more about the library.
  • 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.

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

Where to get the latest version

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

The 0.10 migration guide summarizes the differences between 0.10 and 0.9. To get 0.10.x, add the following to your SBT build:

// available for Scala 2.11, 2.12
libraryDependencies += "co.fs2" %% "fs2-core" % "0.10.4" // For cats 1.1.0 and cats-effect 0.10

// optional I/O library
libraryDependencies += "co.fs2" %% "fs2-io" % "0.10.4"

The previous stable release is 0.9.7. 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:

// available for Scala 2.11, 2.12
libraryDependencies += "co.fs2" %% "fs2-core" % "0.9.7"

// optional I/O library
libraryDependencies += "co.fs2" %% "fs2-io" % "0.9.7"

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

// available for Scala 2.11.8, 2.12.0
libraryDependencies += "co.fs2" %%% "fs2-core" % "0.9.7"

API docs:

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.

  • circe-fs2: Streaming JSON manipulation with circe.
  • doobie: Pure functional JDBC built on fs2.
  • fs2-blobstore: Minimal, idiomatic, stream-based Scala interface for key/value store implementations.
  • fs2-cassandra: Cassandra bindings for fs2.
  • fs2-cron: FS2 streams based on cron expressions.
  • fs2-crypto: TLS support for fs2.
  • fs2-elastic: Simple client for Elasticsearch.
  • fs2-grpc: gRPC implementation for FS2 / Cats Effect.
  • fs2-http: Http server and client library implemented in fs2.
  • fs2-kafka: Simple client for Apache Kafka.
  • fs2-mail: Fully asynchronous java non-blocking email client using fs2.
  • fs2-rabbit: RabbitMQ stream-based client built on top of Fs2.
  • fs2-reactive-streams: A reactive streams implementation for fs2.
  • fs2-redis: Redis stream-based client built on top of Fs2 / Cats Effect.
  • fs2-zk: Simple Apache Zookeeper bindings for fs2.
  • http4s: Minimal, idiomatic Scala interface for HTTP services using fs2.
  • scodec-protocols: A library for working with libpcap files. Contains many interesting pipes (e.g., working with time series and playing back streams at various rates).
  • scodec-stream: A library for streaming binary decoding and encoding, built using fs2 and scodec.
  • streamz: A library that supports the conversion of Akka Stream Sources, Flows and Sinks to and from FS2 Streams, Pipes and Sinks, respectively. It also supports the usage of Apache Camel endpoints in FS2 Streams and Akka Stream Sources, Flows and SubFlows.

Related projects

FS2 has evolved from earlier work on streaming APIs in Scala and Haskell. 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.

Code of Conduct

See the Code of Conduct.

fs2's People

Contributors

mpilquist avatar pchiusano avatar pchlupacek avatar fthomas avatar djspiewak avatar systemfw avatar fiadliel avatar alissapajer avatar yilinwei avatar adamchlupacek avatar zaneli avatar runarorama avatar radekm avatar bryce-anderson avatar rossabaker avatar zainab-ali avatar christopherdavenport avatar shengc avatar changlinli avatar larsrh avatar gvolpe avatar jedesah avatar aeons avatar kaiserpelagic avatar n4to4 avatar bmjames avatar durban avatar xuwei-k avatar mrairing avatar jmcardon avatar

Watchers

James Cloos avatar Mark Canlas avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.