Scala for Multicore

UPMARC Multicore Computing Summer School 2011

Philipp Haller, EPFL and Stanford University

Code Example: Scala Actors

Experimenting with Scala Actors in Scala's interpreter (REPL). Below is both the code entered into the REPL as well as the REPL's output.

import actors.Actor
import Actor._
val summer = actor {
 var sum = 0
 loop {
   receive {
     case ints: Array[Int] => sum += ints.reduceLeft((a, b) => (a+b)%7)
     case from: Actor => from ! sum
   }
 }
}

summer ! Array(1, 2, 3)

val printer = actor {
 loop{
   receive { case res: Int => println(res) }
  }
}

scala> summer ! printer
       6

scala> summer ! Array(3, 3, 3)
scala> summer ! printer
       8