Like futures, we can also compose future using for-comprehensions. In this recipe, we will see how we can do this.
We have already downloaded the akka-agent
dependency in the project, so we will need to just import the project into the IDE.
- Create a scala file,
AgentComposition.scala
, in thecom.packt.chapter4
package. - Add the following imports to the top of the file:
import akka.agent.Agent import scala.concurrent.ExecutionContext.Implicits.global
- Create a test application, as follows:
object AgentComposition extends App { val agent1 = Agent("Hello, ") val agent2 = Agent("World") val finalAgent = for { value1 <- agent1 value2 <- agent2 } yield value1 + value2 println(finalAgent.get) }