Simple Transformations

Simple transformations are stream graph components with one input port and one output port. They “simply” transform a Spout of one type into a Spout of another or even the same type.

A Simple Transformation

Simple transformations are attached to a Spout through a call to the respective DSL method that is defined in the StreamOps trait (see also the StreamOps Scaladoc or StreamOps Source Code).

For example:

import scala.concurrent.Future
import swave.core._

implicit val env = StreamEnv()

val result: Future[String] =
Spout(1, 2, 3, 4, 5) // Spout[Int]
  .map(_ * 2)        // Spout[Int]
  .filter(_ > 5)     // Spout[Int]
  .reduce(_ + _)     // Spout[Int]
  .map(_.toString)   // Spout[String]
  .drainToHead()     // Future[String]

result.value.get.get shouldEqual "24"

To reiterate the explanations from the Quick Start and Basics chapters: Calling a method like .map(...) on a Spout doesn’t trigger any immediate action except for the attachment of a Map stage to the open port of the underlying Spout.
No data start to flow and no elements are actually being mapped.

Only when the stream is started and run will the Map stage spring to life and apply its logic to the incoming data elements.

Synchronous Simple Transformations

Asynchronous Simple Transformations