-
Notifications
You must be signed in to change notification settings - Fork 1
Chains
laforge49 edited this page Sep 9, 2011
·
13 revisions
It can be awkward at time to execute a series of operations.
simpleActor(Prnt(1)) {
rsp1 => {
simpleActor(Prnt(2)) {
rsp2 => {
simpleActor(Prnt(3)) {
rsp3 => {
simpleActor(Prnt("scadoo!"))(rf)
}
}
}
}
}
}
Instead, we can simply create a list of operations and have them run by an actor.
val chain = new Chain
chain.add(simpleActor, Prnt(1))
chain.add(simpleActor, Prnt(2))
chain.add(simpleActor, Prnt(3))
chain.add(simpleActor, Prnt("scadoo!"))
Future(simpleActor, chain)
But things are now always this simple. Quite often we need the results of one operation to create the message used in another.
simpleActor(UltimateAnswer()) {
rsp => {
simpleActor(Prnt(rsp))(rf)
}
}
We can use the Results class to collect results and make them available for subsequent processing. The chain class can then add the result of an operation to the results and also accept a function in place of a message.
val results = new Results
val chain = new Chain(results)
val simpleActor = new SimpleActor
chain.add(simpleActor, UltimateAnswer(), "ultimateAnswer")
chain.addFuncs(
Unit => simpleActor,
Unit => Prnt("The Ultimate Answer to Everything: " +
results("ultimateAnswer").asInstanceOf[Int])
)