I have to write a code such that Actor A produces an infinite stream of numbers which Actor B consumes. Actor A outputs the sequence: x , f(x), g(f(x)) etc where f(x) = 10 if x is 0 and 3x otherwise, and where g(x) is x/2. i.e:
Output: x =0, f(x)=10, g(f(x)=5 (3 messages) then next 3 messages should be f(g(f(x)) , g(f(g(f(x))) , f(g(f(g(f(x)))) and their value...where the inner function becomes x each time to compute the result for the adjacent result
Actor B deals with numbers 3 at a time and it should print each triple tabulated on the same line with the average of the 3 numbers.
The value (0) is passed to ActorA from the main method.
My attempt:
import akka.actor._
class ActorA(processB:ActorRef) extends Actor with ActorLogging{
def f(x : Int) = if(x == 0) 10 else 3 * x
def g(x : Int) = x / 2
def receive = {
case 0 =>
val x = 0
//processB ! (x)
// processB ! (f(x))
// processB ! (g(f(x)))
println( (f(x)))
println((g(f(x))))
/*case Stop =>
Console.println("Stop")
processB ! Stop
context stop self */
}
}
class ActorB extends Actor with ActorLogging{
def receive = {
case ActorA =>
case Stop =>
Console.println("Stop")
exit()
}
}
case object ActorA
case object ActorB
case object Stop
object messages {
def main(args: Array[String]) :Unit = {
val system = ActorSystem("actors")
val processB = system.actorOf(Props[ActorB])
val actorA = system.actorOf(Props(new ActorA(processB)))
actorA ! 0
}
}
How to produce an INFINITE number of messages and can I deal with them 3 at a time? Thanks