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
@seanparsons
seanparsons / gist:914305
Created April 11, 2011 20:47
Easy parsing using regexes and pattern matching.
// The variable data could easily be an Iterator from scala.io.Source instance, it will work just the same.
val data = List("Food,Count","Cake,9","Biscuits,2","Cookie,10")
val Parser = """([^,]+),(\d+)""".r
val entries = data.collect{case Parser(food,count) => (food, count.toInt)}
case class Button()
case class AlertDialog(
title: String = "",
message: String = "",
positiveButton: Option[Button] = None
)
def builder = new AlertDialog(title = "Epic Builder")
@seanparsons
seanparsons / gist:952167
Created May 2, 2011 19:13
Early implementation of the command line parser in my app server.
sealed abstract class CommandLineItem(name: String, description: String)
case class CommandLineArgument(name: String, description: String) extends CommandLineItem(name, description)
case class CommandLineOption(name: String, description: String, optionName: String) extends CommandLineItem(name, description)
case class PassedInItems(options: Map[String, String] = Map(), arguments: List[String] = List(), hangingOptionName: Option[String] = None)
case class CommandLineOptions(commandLineOptions: Seq[CommandLineOption], commandLineArguments: Seq[CommandLineArgument]) {
def parse(args: Array[String]): Either[PassedInItems, String] = {
args.foldLeft(Left(PassedInItems()).asInstanceOf[Either[PassedInItems, String]]){(workingResult, arg) => workingResult match {
case Left(PassedInItems(options, arguments, hangingOptionName)) => {
@seanparsons
seanparsons / gist:952216
Created May 2, 2011 19:37
Use of the command line parser.
object Server {
def main(args: Array[String]): Unit = {
val serviceName = "appserver"
val managerPortOption = new CommandLineOption("Manager Port", "Manager Port", "managerPort")
val serverPortOption = new CommandLineOption("Server Port", "Server Port", "serverPort")
val options = List(managerPortOption, serverPortOption)
val arguments = List(new CommandLineArgument("Command", "Command"))
def success(options: Map[String, String], arguments: Seq[String]): Unit = {
val serverPort = options(serverPortOption.optionName).toInt
val managerPort = options(managerPortOption.optionName).toInt
@seanparsons
seanparsons / gist:964759
Created May 10, 2011 16:02
Akka ASCII art!
05/10 17:01:35 INFO [main] a.s.AkkaLoader - ==================================================
05/10 17:01:35 INFO [main] a.s.AkkaLoader - t
05/10 17:01:35 INFO [main] a.s.AkkaLoader - t t t
05/10 17:01:35 INFO [main] a.s.AkkaLoader - t t tt t
05/10 17:01:35 INFO [main] a.s.AkkaLoader - tt t t tt t
05/10 17:01:35 INFO [main] a.s.AkkaLoader - t ttttttt t ttt t
05/10 17:01:35 INFO [main] a.s.AkkaLoader - t tt ttt t ttt t
05/10 17:01:35 INFO [main] a.s.AkkaLoader - t t ttt t ttt t t
05/10 17:01:35 INFO [main] a.s.AkkaLoader - tt t ttt ttt ttt t
05/10 17:01:35 INFO [main] a.s.AkkaLoader - t t ttt ttt t tt t
@seanparsons
seanparsons / gist:973122
Created May 15, 2011 12:49
Static Web Server
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
@Grab(group="org.apache.camel",module="camel-jetty",version="2.7.0")
@Grab(group="org.slf4j",module="slf4j-nop",version="1.6.1")
class WebRouteBuilder extends RouteBuilder {
void configure() {
from("jetty:http://0.0.0.0/cake")
.inOut()
.setBody(constant("<html><title>Cake</title><body>Chocolate Cake Is The Best</body></html>"))
@seanparsons
seanparsons / gist:1020352
Created June 11, 2011 07:50
Early results from my HTTP server with ApacheBench
Server Software: Gigantor!
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /dave
Document Length: 44 bytes
Concurrency Level: 200
Time taken for tests: 5318.533 seconds
Complete requests: 10000000
@seanparsons
seanparsons / gist:1029056
Created June 16, 2011 11:16
Weekly Scala Tip: As indiciated by @mariofusco here: http://twitter.com/mariofusco/status/81308217346506752 we should eliminate throwing exceptions up as much as possible.
scala> import scala.util.control._
import scala.util.control._
scala> Exception.catching(classOf[Throwable]).either{"1".toInt}
res2: Either[Throwable,Int] = Right(1)
scala> Exception.catching(classOf[Throwable]).either{"dave".toInt}
res3: Either[Throwable,Int] = Left(java.lang.NumberFormatException: For input string: "dave")
@seanparsons
seanparsons / gist:1069838
Created July 7, 2011 15:58
Basic timed property for use with ScalaCheck.
import org.scalacheck.Prop
import org.scalacheck.Prop.Params
import java.util.concurrent.atomic.AtomicBoolean
class TimedProp(maximumMs: Int, wrappedProp: => Prop) extends Prop {
def apply(prms: Params) = {
val atLeastOnce = new AtomicBoolean(true)
val startTime = System.currentTimeMillis()
val resultStream = Stream.continually{
Thread.sleep(20)
@seanparsons
seanparsons / gist:1122882
Created August 3, 2011 15:14
Quick example of a payment process done with Akka.
import akka.actor._
import akka.actor.Actor._
case class StartPayment(provider: String)
case class CompletePayment(provider: String)
case class PaymentProcess(userId: Int) extends Actor {
var currentProvider: String = _
def receive = {
case StartPayment(provider) => {