Skip to main content

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 {

except that it performs better on multi-core computers.


Popular posts from this blog


Somebody asked me a question about my Tooth Fairy post the other day that got me thinking. How many baby teeth are lost every day in Utah? I began with Googling. Surely someone else has thought of this and run some numbers, right? Lo, there is a tooth fairy site that claims that the Tooth Fairy collects 300,000 teeth per night . That's a lot; however, when I ran the numbers, it started to seem awfully low. Let's assume that the Tooth Fairy collects all baby teeth regardless of quality and we assume that all children lose all their baby teeth. The world population of children sits at 2.2 billion , with 74.2 million of them in the United States. Of those, approximately 896,961 of them are in Utah . This means that somewhere around .04077% of the world's children are in Utah. If we assume that kids in Utah lose teeth at the same rate as all other children in the world and that each day in the year is just as likely as the rest to lose a tooth, then we have that of

BYU and the Sunday Compromise?

I read an article by Brad Rock this morning where he quoted heavily from Dr. Thomas Forsthoefel who was giving his opinion on religious institutions being involved in sports . BYU , of course, came up. I think Forsthoefel came off sounding a bit misinformed about the culture, drive, mission, etc. of BYU . Below is the email that I sent to Brad Rock this morning after finishing the article: Brad - That was an interesting article. I tend to disagree with Forsthoefel, though, or at least disagree with what I may have read into his comments. A quote in your article says: "There may be a kind of growing pain. BYU is in the real world and the real world works on Sunday. Can we (BYU) live with the adjustment? I'm empathetic with that, whatever decision is made, people are going to be unhappy.… Some will say get with the program, we'll be OK at the next level, others will say we've sold out and we've made a deal with the world." This seems to suggest one o


If you read my previous post on my sons' names, you'll know that this post is about my daughters' names. When we found out that we were going to have twins, I vowed that there names were not going to rhyme or alliterate. We weren't going to do Jadyn and Jordan, or Kim and Tim, or Esther and Edgar (all likely candidates for other, less elitist parents, especially Esther and Edgar). I did want the names to have something to do with one another somehow. Felicity Mae Cummings Felicity's first name has little to do with its underlying Hebrew meaning or its tie to Biblical history and everything to do with the fact that this was a name that Kristi had always wanted one of her girls to have because she liked that it meant "happiness". So, to tell you the truth, I didn't do a lot of research on this name because its place in our family had already been decided. But, it was excellent material to work with. The initial spark that 'Felicity' pro