Created
September 8, 2013 03:39
-
-
Save liancheng/6481638 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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