Skip to content

Instantly share code, notes, and snippets.

@adbrowne
Created April 22, 2011 08:32
Show Gist options
  • Save adbrowne/936274 to your computer and use it in GitHub Desktop.
Save adbrowne/936274 to your computer and use it in GitHub Desktop.
Hello World scala actors
import scala.actors._
import scala.actors.Actor._
class Message(val message: String = "")
val helloActor = actor {
var nextActor : Option[Actor] = None
while(true) {
receive {
case a : Actor => nextActor = Option(a)
case msg : Message => nextActor match
{
case Some(a: Actor) => a ! new Message(msg.message + "Hello")
case None => {}
}
}
}
}
val worldActor = actor {
var nextActor : Option[Actor] = None
while(true) {
receive {
case a : Actor => nextActor = Option(a)
case msg : Message => nextActor match
{
case Some(a: Actor) => a ! new Message(msg.message + " World")
case None => {}
}
}
}
}
helloActor ! worldActor
worldActor ! actor {
while(true) {
receive {
case msg : Message => println(msg.message)
}
}
}
helloActor ! new Message
while(true){}
import scala.actors._
import scala.actors.Actor._
val helloActor = actor {
receive {
case nextActor : Actor => {
println("Hello Actor linked to " + nextActor)
while(true) {
receive {
case message : String => {
println("Hello Actor receives message (\""+ message + "\")")
nextActor ! message + " Hello"
}
}
}
}
}
}
val worldActor = actor {
receive {
case nextActor : Actor => {
println("World Actor linked to " + nextActor)
while(true) {
receive {
case message : String => {
println("World Actor receives message (\""+ message + "\")")
nextActor ! message + " World"
}
}
}
}
}
}
val printActor = actor {
while(true) {
receive {
case message: String => println("Print Actor receives message (\""+ message + "\")")
}
}
}
println("Send link messages")
helloActor ! worldActor
worldActor ! printActor
println("Sent messages")
helloActor ! "I say"
println("ok")
readLine
import scala.actors._
import scala.actors.Actor._
val helloActor : Actor = actor {
var nextActor : Option[Actor] = None
receive {
case a : Actor => {
nextActor = Some(a)
println("Hello Actor "+ helloActor +" linked to " + a)
}
}
while(true) {
receive {
case message : String => {
println("Hello Actor "+ helloActor +" receives message (\""+ message + "\")")
nextActor.get ! message + " Hello"
}
}
}
}
val worldActor : Actor = actor {
var nextActor : Option[Actor] = None
receive {
case a : Actor => {
nextActor = Some(a)
println("World Actor "+ worldActor +" linked to " + a)
}
}
while(true) {
receive {
case message : String => {
println("World Actor "+ worldActor +" receives message (\""+ message + "\")")
nextActor.get ! message + " World"
}
}
}
}
val printActor : Actor = actor {
while(true) {
receive {
case message: String => println("Print Actor "+ printActor +" receives message (\""+ message + "\")")
}
}
}
println("Send link messages")
helloActor ! worldActor
worldActor ! printActor
println("Sent messages")
helloActor ! "I say"
println("ok")
readLine
// this is the simplest thing that would compile and run.
// no guarantees that it'll work in all cases
object ThreadHelper{
private val t1 = new ThreadLocal[Actor]
def start(actor: Actor) = {
(new Thread(){
override def run {
t1.set(actor)
actor.act
}
}).start
actor
}
def getActor = t1.get
}
object Actor{
def actor(body: => Unit) : Actor = {
val a = new Actor{
def act = body
}
ThreadHelper.start(a);
}
def receive(respond: PartialFunction[Any, Any]) = {
var actor = ThreadHelper.getActor
while(actor.messageQueue.isEmpty){ }
var nextMessage = actor.messageQueue.dequeue
if(respond.isDefinedAt(nextMessage)) respond(nextMessage)
}
}
trait Actor{
def act
val messageQueue = new scala.collection.mutable.Queue[Any]
def ! (message: Any) = {
messageQueue.enqueue(message)
}
}
import Actor._;
val helloActor : Actor = actor {
var nextActor : Option[Actor] = None
receive {
case a : Actor => {
nextActor = Some(a)
println("Hello Actor "+ helloActor +" linked to " + a)
}
}
while(true) {
receive {
case message : String => {
println("Hello Actor "+ helloActor +" receives message (\""+ message + "\")")
nextActor.get ! message + " Hello"
}
}
}
}
val worldActor : Actor = actor {
var nextActor : Option[Actor] = None
receive {
case a : Actor => {
nextActor = Some(a)
println("World Actor "+ worldActor +" linked to " + a)
}
}
while(true) {
receive {
case message : String => {
println("World Actor "+ worldActor +" receives message (\""+ message + "\")")
nextActor.get ! message + " Hello"
}
}
}
}
val printActor : Actor = actor {
while(true) {
receive {
case message: String => println("Print Actor "+ printActor +" receives message (\""+ message + "\")")
}
}
}
println("Send link messages")
helloActor ! worldActor
worldActor ! printActor
println("Sent messages")
helloActor ! "I say"
println("ok")
readLine
Send link messages
Hello Actor scala.actors.Actor$$anon$1@1f3785d3 linked to scala.actors.Actor$$anon$1@7632efa7
Sent messages
ok
World Actor scala.actors.Actor$$anon$1@7632efa7 linked to scala.actors.Actor$$anon$1@2565a3c2
Hello Actor scala.actors.Actor$$anon$1@1f3785d3 receives message ("I say")
World Actor scala.actors.Actor$$anon$1@7632efa7 receives message ("I say Hello")
Print Actor scala.actors.Actor$$anon$1@2565a3c2 receives message ("I say Hello Hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment