Skip to content

Instantly share code, notes, and snippets.

@liancheng
Created September 8, 2013 03:39
Show Gist options
  • Save liancheng/6481638 to your computer and use it in GitHub Desktop.
Save liancheng/6481638 to your computer and use it in GitHub Desktop.
import scala.actors._
import Actor._
def sleepSort(xs : List[Int]) : List[Int] = {
def sorter(aggregator : Actor, n : Int) = actor {
reactWithin(n * 1000) {
case TIMEOUT => {
aggregator ! n
exit
}
}
}
xs map (sorter(self, _)) foreach (_.start)
val inputLength = xs.length
def aggregate(result : List[Int], resultLength : Int) : List[Int] =
if (resultLength == inputLength) {
result.reverse
} else receive {
case n : Int =>
aggregate(n :: result, resultLength + 1)
}
aggregate(List[Int](), 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment