Technical Setup

A great way to learn fs2 is to use ammonite (a scala repl). Ammonite allows you to keep a list of scala expressions and dependent libraries in a separate scala file and import them.

I keep a set of imports around that I include when needed:

$ cat myfs2.sc
import $ivy.`co.fs2::fs2-core:0.9.5`
import $ivy.`co.fs2::fs2-io:0.9.5`
repl.prompt() = "fs2> "
import scala.concurrent.ExecutionContext.Implicits.global
// needed due to a bug in scala based bug repl environments
@ repl.compiler.settings.Ydelambdafy.value = "inline"
import fs2._
import fs2.util._

implicit val strategy = Strategy.fromExecutionContext(global)
// for scalajs, you can use Scheduler.default
implicit val scheduler = Scheduler.fromFixedDaemonPool(2, "fs2-wokers")
// You can also just do import Task._
implicit val F = Async[Task]

You can also use latest.version or latest.release instead of an explicit version number such as 0.9.1. With this .sc file you can just run the console:

$ amm
@

then run the command to lead the imports:

@ import $exec.myfs2 
Compiling myfs2.sc
import $exec.$
fs2> strategy
res1: Strategy = strategy

Notice the prompt change via the fs2.sc file so you know what you imported. You can now easily access fs2 and the imports present in the myfs2.sc will be available in your repl session. This is due to the use of "$exec" versus "$file" in amm.

If you use the scala-ide in eclipse, you can also open a scala worksheet and have the outputs automatically appended to the end of the scala code lines. This is an alternative to using the sbt console. The scala worksheet output looks different than the raw REPL (note this still shows scalaz-streams):

  Process.emit(0)                                 //> res0: scalaz.stream.Process0[Int] = Emit(Vector(0))

You may also be able to get a scala notebook interface running using jyputer or another notebook implementation.

The Async[Task] brings an implicit Async strategy design object into implicit scope. It can be used to abstractly provide asynchronous building blocks without committing to a particular effect. fs2 uses this through the library to provide capabilities independent of the effect. There is an implicit in Task's companion object which can be imported using import Task._.

Last updated