This page is no longer maintained — Please continue to the home page at www.scala-lang.org

http://stackoverflow.com/questions/8891732/can-we-define-a-set-of-dsl-operation-in-scala-that-perform-parallelly-with-each

1 reply
Liu Yongjian
Joined: 2012-02-04,
User offline. Last seen 42 years 45 weeks ago.
DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: http://stackoverflow.com/questions/8891732/can-we-define-a-s

Scala doesn't support that, but you could emulate it with actors.

pipelineparallelcombinators.scala
=================================
package pipelineparallelcombinators

import java.util.concurrent.CountDownLatch
import akka.actor._
import akka.actor.Actor._

case class Message(elem: String)
case object StopMessage

object ActorManager extends App {
val lst = List("I", "am", "a" , "student", ".", "I", "come", "from",
"China", ".","I","love","peace")

val filterRef = actorOf(new FilterActor())
val mapRef = actorOf(new MapActor())
val foreachRef = actorOf(new ForeachActor())

val latch = new CountDownLatch(3)

filterRef.start
mapRef.start
foreachRef.start

var start = System.currentTimeMillis
for (elem <- lst) filterRef ! Message(elem)

filterRef ! StopMessage

latch.await

println ("elapsed = " + (System.currentTimeMillis - start) + " ms")

}

class FilterActor extends Actor {
def receive = {
case Message(elem) => {
println("a:" + elem)
if(elem == "I") ActorManager.mapRef ! Message(elem)
}
case StopMessage => {
ActorManager.mapRef ! StopMessage
ActorManager.latch.countDown
self ! PoisonPill
}
}
}

class MapActor extends Actor {
def receive = {
case Message(elem) => {
println("b:" + elem)
ActorManager.foreachRef ! Message(elem.toLowerCase)
}
case StopMessage => {
ActorManager.foreachRef ! StopMessage
ActorManager.latch.countDown
self ! PoisonPill
}
}
}

class ForeachActor extends Actor {
def receive = {
case Message(elem) => {
println("c:" + elem)
}
case StopMessage => {
ActorManager.latch.countDown
self ! PoisonPill
}
}
}

C:\scala-2.10.0-M1\examples>scala
pipelineparallelcombinators.ActorManager
AKKA_HOME is defined as [c:\akka-actors-1.1.2], loading config from [c:
\akka-act
ors-1.1.2/config/akka.conf].
a:I
a:am
a:a
a:student
b:I
a:.
a:I
a:come
a:from
a:China
a:.
c:i
a:I
a:love
a:peace
b:I
b:I
c:i
c:i
elapsed = 31 ms

On 4 feb, 16:44, Liu Yongjian wrote:
> This is a question I posted on stackflow.comhttp://stackoverflow.com/questions/8891732/can-we-define-a-set-of-dsl...

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland