Skip to content

Instantly share code, notes, and snippets.

View seanparsons's full-sized avatar
💭
Setting A Status Message

Sean Parsons seanparsons

💭
Setting A Status Message
View GitHub Profile
object Main {
def main(args: Array[String]): Unit = {
new SixtyEightK() {
moveq(10, d0)
addl(200, d0)
println(d0)
// Outputs: DataRegister(210)
}
}
}
class IntMatchActor extends Actor {
def receive = {
case 1 => self.reply("This is number 1.")
case 2 => self.reply("This is number 2, it comes after 1.")
case _ => self.reply("This value is neither 1 nor 2.")
}
}
val intMatchActor = actorOf[IntMatchActor].start
println(intMatchActor !! 1)
val firstLongProcessActor = actorOf[FirstLongProcessActor].start
val secondLongProcessActor = actorOf[SecondLongProcessActor].start
val startTime = currentTimeMillis
val firstFuture = firstLongProcessActor !!! 1000
val secondFuture = secondLongProcessActor !!! 1000
Futures.awaitAll(List(firstFuture, secondFuture))
val timeTaken = currentTimeMillis - startTime
println("Process took " + timeTaken + "ms.")
// Output is:
val supervisor = Supervisor(SupervisorConfig(
AllForOneStrategy(List(classOf[IllegalStateException]), Some(3), Some(1000)),
Supervise(actorOf[BrokenActor], Permanent) :: Nil
))
supervisor.start
val brokenActor = supervisor.children.head
printlnRequestResult(brokenActor, 1)
printlnRequestResult(brokenActor, 5)
try {
printlnRequestResult(brokenActor, 20)
@seanparsons
seanparsons / gist:812488
Created February 5, 2011 14:30
Local Map-Reduce in less than 60 lines of code.
import java.util.concurrent.atomic.AtomicInteger
import akka.actor._
import akka.actor.Actor._
import akka.dispatch.Future
case class Reduce(val size: Int)
case class MappingDetail[T](val item: T, val reducer: ActorRef)
class MapperActor[T, U](val mapping: (T) => U) extends Actor {
"Vector" should {
"return a distance of 2 in a 1-dimensional difference" in {
val distance = new PositionVector(0, 0, 0).distance(new PositionVector(2, 0, 0))
distance must equalTo(2)
}
@seanparsons
seanparsons / gist:836435
Created February 20, 2011 23:51
Example visualizer code for Akka commits.
import scala.io.Source
import scala.xml._
import scala.collection.mutable._
val source = Source.fromURL("""http://query.yahooapis.com/v1/public/yql?q=select%20commit.author.name,%20commit.id%20from%20github.repo.commits%20where%20id='jboner'%20and%20repo='akka'&diagnostics=true&env=store://datatables.org/alltableswithkeys""")
val sourceXML = XML.loadString(source.mkString)
val commits = sourceXML \ "results" \ "commits" \ "commit"
val authorsToCommits: MultiMap[String, String] = commits.foldLeft(new HashMap[String, Set[String]] with MultiMap[String, String]){(multiMap,commit) =>
multiMap.addBinding((commit \ "author" \ "name").text, (commit \ "id").text)
}
scala> (connection.get("TEST").liftFailNel |@| connection.get("TEST2").liftFailNel){(test, test2) => List(test,test2)}
res5: scalaz.Validation[scalaz.NonEmptyList[String],List[Option[String]]] = Success(List(Some(5), Some(6)))
scala> connection.close()
scala> (connection.get("TEST").liftFailNel |@| connection.get("TEST2").liftFailNel){(test, test2) => List(test,test2)}
res7: scalaz.Validation[scalaz.NonEmptyList[String],List[Option[String]]] = Failure(NonEmptyList(Socket closed, Socket closed))
import org.apache.camel.Processor
import org.apache.camel.Exchange
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.commons.io.FileUtils
import java.net.URL
@Grab(group="org.apache.camel",module="camel-jetty",version="2.5.0")
@Grab(group="commons-io", module="commons-io",version="2.0")
class WebRouteBuilder extends RouteBuilder {
@seanparsons
seanparsons / gist:900724
Created April 3, 2011 20:08
Difference between call by name and normal parameters in Scala.
val counter = new java.util.concurrent.atomic.AtomicInteger(0)
def printTwiceExpression(expression: => Int) = {
println(expression)
println(expression)
}
def printTwiceValue(value: Int) = {
println(value)
println(value)
}