Friday, June 17, 2011

Sleep Sort! With Actors! For No Reason!

The world's stupidest, racy sort algorithm ported to Scala actors for no good reason.

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

   xs map (sorter(self, _)) foreach (_.start)

   val inputLength = xs.length

   def aggregate(result : List[Int], resultLength : Int) : List[Int] = 
     if (resultLength == inputLength) {
     } else receive {
        case n : Int => 
          aggregate(n :: result, resultLength + 1)

   aggregate(List[Int](), 0)

Example usage:

scala> sleepSort(List(3,1,2))
res0: List[Int] = List(1, 2, 3)

Caveats: Can take a shockingly long time. Doesn't handle negative numbers. Doesn't handle actor failures. Races are possible. If you try to use this in real code then you will be fired and possibly shot.


Josh Suereth said...

WOW!  That's the best scaling implementation of sleep-sort I've seen.  It would only be better if you had a eBPM rule-based implementation. Then it would be fast *AND* smart.

James Iry said...


Llantianheyu said...

Discount Oakley Sunglasses Discount provide rethgjkg attractiveness in addition to glamour on your existence they usually symbolize some sort of enjoyment in addition to fashionable term of forms personality. The pattern of the people Cheap Oakley Sunglasses is modern-day in addition to splendid. For individuals who want to appear good like for example , the varieties of Oakley Frogskins they must obtain intended for themselves. A great inexpensive sum of cash Oakley Jawbone will productively guard you throughout the vibrant summertime sunlight fully and fashionably. These are the perfect vision gears for you if you fully knowledge that imaginative and prescient vision gears usually are some on the initial recognizable items in relation to you.An standard element that has an effect on the Oakley Jawbone Sunglasses a number of kids stands out as the consequence on the preferred personas. Oakley sunglasses donned by means of cartoon results in telly have enticed a big quantity connected with kids. motion pictures and catalogs may perform the roles in securing youthful young children in buying Oakley sunglasses. Brad Peter is usually a single on the most remarkable examples.