Skip to content

Instantly share code, notes, and snippets.

View bdarfler's full-sized avatar

Ben Darfler bdarfler

View GitHub Profile
package com.mycompany
import org.scalatra._
class Uploader extends ScalatraFilter {
get("/test"){
status ( 202 )
}
}
package com.mycompany
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers
import org.scalatra.test.ScalatraTests
class UploaderTest extends FunSuite with ShouldMatchers with ScalatraTests{
routeFilter(classOf[Uploader], "/*")
test("simple get") {
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<!-- =============================================================== -->
<!-- Configure the Jetty Server -->
<!-- -->
<!-- Documentation of this file format can be found at: -->
<!-- http://docs.codehaus.org/display/JETTY/jetty.xml -->
<!-- -->
<!-- =============================================================== -->
@bdarfler
bdarfler / gist:922118
Created April 15, 2011 17:46
castToInt
def castToInt(any: Option[Any]): Option[Int] = {
any match {
case Some(value: Int) => Some(value)
case _ => None
}
}
@bdarfler
bdarfler / gist:1036753
Created June 20, 2011 22:24
Split Collection
@tailrec
def splitHelper[T](l : List[T], b: ListBuffer[List[T]])(c: T => Boolean) : List[List[T]] = {
l match {
case Nil => b.result()
case h :: t =>
val (h, t) = l.tail.span(c)
b += l.head :: h ::: Nil
splitHelper(t, b)(c)
}
}
import java.io.ByteArrayInputStream
import java.util.zip.GZIPOutputStream
import java.util.zip.GZIPInputStream
import java.io.ByteArrayOutputStream
val buffer = new Array[Byte](1024*4)
def gzip(bytes: Array[Byte]) = {
val byteIn = new ByteArrayInputStream(bytes)
val byteOut = new ByteArrayOutputStream()
@bdarfler
bdarfler / microbenchmark-runner.scala
Created September 8, 2011 15:27
Scala Microbenchmark Runner
def benchmark(runs: Long, reps: Long, funct: => Any) = {
// warmup
println("Warming Up")
var i = 0
while ( i < 25000 ) { funct; i = i + 1 }
// test runs
println("Running Test")
var j = 0
while ( j < runs ) {
var k = 0
@bdarfler
bdarfler / gist:1217744
Created September 14, 2011 20:47
Broken REPL
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val builder = Set.newBuilder[Array[Byte]]
builder: scala.collection.mutable.Builder[Array[Byte],scala.collection.immutable.Set[Array[Byte]]] = scala.collection.mutable.AddingBuilder@22509bfc
scala> for(_ <- 0 until 10) builder += Array[Byte](1,2,3)
scala> builder.result()
@bdarfler
bdarfler / gist:1268240
Created October 6, 2011 18:39
Exclude Depenency
// Needed to gain control over which jackson version we use
override def ivyXML =
<dependencies>
<dependency org="com.amazonaws" name="aws-java-sdk" rev="1.2.8">
<exclude module="jackson-core-asl"/>
</dependency>
</dependencies>
@bdarfler
bdarfler / gist:1632845
Created January 18, 2012 12:46
Scala Snake YAML
import scalaj.collection.Imports._
import org.yaml.snakeyaml.Yaml
def parse(str: String) = {
new Yaml().load(str) match {
case obj: ArrayList[_] =>
val seqOfMaps = obj.asScala.collect {
case hashMap: HashMap[_, _] =>
hashMap.asScala.toMap.asInstanceOf[Map[String, Any]]
}.toSeq