Hello World!

You are viewing the documentation for the older Monix 2.x series.
For the latest version: see here!

Let’s do instant gratification stuff.

First, we need a Scheduler whenever asynchronous execution happens.

// We need a scheduler whenever asynchronous
// execution happens, substituting your ExecutionContext
import monix.execution.Scheduler.Implicits.global

// Needed below
import scala.concurrent.Await
import scala.concurrent.duration._

Task, the Lazy Future #

For using Task or Coeval, usage is fairly similar with Scala’s own Future, except that Task behaves lazily:

import monix.eval._

// A specification for evaluating a sum,
// nothing gets triggered at this point!
val task = Task { 1 + 1 }

Task in comparison with Scala’s Future is lazy, so nothing gets evaluated yet. We can convert it into a Future and evaluate it:

// Actual execution, making use of the Scheduler in
// our scope, imported above
val future = task.runAsync
// future: monix.execution.CancelableFuture[Int] = monix.execution.CancelableFuture$Implementation@1273dc22

We now have a standard Future, so we can use its result:

// Avoid blocking; this is done for demostrative purposes
Await.result(future, 5.seconds)
// res0: Int = 2

Observable, the Lazy & Async Iterable #

We already have the Scheduler in our local scope, so lets make a tick that gets triggered every second.

import monix.reactive._

// Nothing happens here, as observable is lazily
// evaluated only when the subscription happens!
val tick = {
  Observable.interval(1.second)
    // common filtering and mapping
    .filter(_ % 2 == 0)
    .map(_ * 2)
    // any respectable Scala type has flatMap, w00t!
    .flatMap(x => Observable.fromIterable(Seq(x,x)))
    // only take the first 5 elements, then stop
    .take(5)
    // to print the generated events to console
    .dump("Out")
}

Our observable is at this point just a specification. We can evaluate it by subscribing to it:

// Execution happens here, after subscribe
val cancelable = tick.subscribe()
// 0: Out-->0
// 1: Out-->0
// 2: Out-->4
// 3: Out-->4
// 4: Out-->8
// 5: Out completed

// Or maybe we change our mind
cancelable.cancel()

Isn’t this awesome? (✿ ♥‿♥)