Created
November 14, 2017 15:25
-
-
Save ygrenzinger/eb72fe0ea58e1d7958cd89d6217d4656 to your computer and use it in GitHub Desktop.
Carbon IT afternoon discovering Akka Actors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.{Actor, ActorRef, ActorSystem, Props} | |
trait Strings { | |
val START = "Let the fun begin!" | |
val NOINT = "Please give me an Int" | |
val ASK = "Factorial?" | |
implicit class Format(s: String) { | |
def isInt: Boolean = s.forall(_.isDigit) | |
} | |
} | |
class FactorialActor extends Actor { | |
def factorial(n: Int): BigInt = (1 to n).foldLeft(1: BigInt)(_ * _) | |
def receive: PartialFunction[Any, Unit] = { | |
case i: Int => sender() ! factorial(i) | |
} | |
} | |
class ParsingActor extends Actor with Strings { | |
val functorialActor: ActorRef = context.actorOf(Props(new FactorialActor()), name = "functorial-actor") | |
def receive: PartialFunction[Any, Unit] = { | |
case r: BigInt => println("Factorial result :" + r); self ! "ask" | |
case x: String if x.isInt => functorialActor ! x.toInt | |
case "start" => println(START); self ! "ask" | |
case "ask" => println(ASK); self ! io.StdIn.readLine | |
case "exit" => System.exit(1) | |
case _ => println(NOINT); self ! "ask" | |
} | |
} | |
object Console { | |
val system = ActorSystem("Carbon IT System") | |
val parsingActor: ActorRef = system.actorOf(Props(new ParsingActor()), name = "parsing-actor") | |
def main(av: Array[String]): Unit = { | |
parsingActor ! "start" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment