28 September 2009

Scala: Multi-core for loop

Two of the things that really attract me to Scala are its immutability and threading approaches. It seems to me that both of these make Scala a great candidate for multi-core and cloud-friendly constructs.

Here is what I developed to help me write a multi-threaded iterator (of course, these are only used when the order of execution for each element doesn't matter):



def fire[A,B](expanded:A=>B):Fire[A,B] = new Fire[A,B](expanded);

class Fire[A,B](val expanded:A=>B) {
def forall(list:Iterable[A]):Any = {
var caller = self;
list foreach {
el =>
actor {
caller ! expanded(el)
}
}

list map {
el =>
receive {
case result:B => doReceive(result)
}
}
}

protected def doReceive(result:B):Any = {

}

def andlisten[C](contracted:B=>C):FireAndListen[A,B,C] = new FireAndListen(expanded, contracted)
}

class FireAndListen[A,B,C](expanded:A=>B, contracted:B=>C) extends Fire[A,B](expanded) {
override def doReceive(result:B):C = contracted(result);
}



Thus, you get something that looks like this:


fire {
myVar:myType => myMethod(myVar1)
} andlisten { //optional
myVar2 => myMethod2(myVar2) // the type of myVar2 is the return of the previous method. If the return type is Unit, then this section is unnecessary
} forall myList


This is effectively the same as:


myList foreach {
myMethod2(myMethod(myVar1))
}


except that it performs better on multi-core computers.