Skip to content

Instantly share code, notes, and snippets.

@kings13y
kings13y / FizzBuzzScalaCheck.scala
Created May 28, 2012 10:51
FizzBuzz spec using ScalaCheck
package org.scalabound.scatdd.scalacheck
import org.scalabound.scatdd.FizzBuzz
import org.scalacheck.ConsoleReporter.testReport
import org.scalacheck.Prop.forAll
import org.scalacheck.Prop.propBoolean
import org.scalacheck.ConsoleReporter
import org.scalacheck.Test
object FizzBuzzScalaCheck {
@kings13y
kings13y / FizzBuzzScalaTest.scala
Created May 28, 2012 10:50
FizzBuzz Test spec with ScalaTest
package org.scalabound.scatdd.scalatest
import org.junit.runner.RunWith
import org.scalatest.FunSpec
import org.scalatest.junit.JUnitRunner
import org.scalabound.scatdd.FizzBuzz
@RunWith(classOf[JUnitRunner])
class FizzBuzzScalaTest extends FunSpec {
@kings13y
kings13y / FizzBuzzSpecs2.scala
Created May 28, 2012 10:25
FizzBuzz test case in Specs2
package org.scalabound.scatdd.specs2
import org.scalabound.scatdd.FizzBuzz
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
// package unitTest { |> Scala IDE will not pick the class up as runnable if I sub-package it like this :-(
@RunWith(classOf[JUnitRunner])
class FizzBuzzJUnitSpec extends org.specs2.mutable.Specification {
@kings13y
kings13y / FizzBuzz.scala
Created May 28, 2012 10:06
Minimal FizzBuzz implementation in Scala
package org.scalabound.scatdd
object FizzBuzz {
def eval(x: Int) = (x % 3, x % 5, x) match {
case(0, 0, _) => "FizzBuzz"
case(0, _, _) => "Fizz"
case(_, 0, _) => "Buzz"
case _ => "" + x
}
@kings13y
kings13y / FizzBuzzJUnitTest.java
Created May 28, 2012 10:04
FizzBuzz Java JUnit test
package org.scalabound.scatdd;
import static org.junit.Assert.*;
import org.junit.Test;
import org.scalabound.scatdd.FizzBuzz;
public class FizzBuzzJUnitTest {
@kings13y
kings13y / gist:2136456
Created March 20, 2012 14:51
mvn archetype quickstart commands for Scala
mvn archetype:generate -DarchetypeGroupId=org.scala-tools.archetypes -DarchetypeArtifactId=scala-archetype-simple
mvn compile
mvn exec:java -Dexec.mainClass="some.path.to.Main" -Dexec.args="arg0 arg1 arg2"
@kings13y
kings13y / MinimalRestServer.scala
Created August 9, 2011 22:05
Sample Rest style service using just Scala and Java 6 annotations
package output
import javax.xml.bind.{ Marshaller, JAXBContext }
import javax.xml.bind.annotation._
import javax.xml.bind.util.JAXBSource
import javax.xml.transform.Source
import javax.xml.ws.{ Endpoint, Provider, ServiceMode, WebServiceContext, WebServiceProvider }
import javax.xml.ws.http.HTTPBinding
import javax.xml.ws.handler.MessageContext
import javax.annotation.Resource
@kings13y
kings13y / PartialUpdateToImmutableObject.scala
Created July 26, 2011 08:06
Partial updates with immutable domain objects
case class Person(val firstName: String, val lastName: String, val age: Int, val email: String) {
def update(firstName: String = firstName, lastName: String = lastName, age: Int = age, email: String = email) : Person = {
Person(firstName, lastName, age, email)
}
}
val seedPerson = Person("A", "B", 1, "me@home.com")
println(seedPerson)
val updatedPerson = seedPerson update (age = 100, firstName = "Z")
@kings13y
kings13y / ReifiedManifest.scala
Created June 16, 2011 22:45
Reification via a Manifest example
class ReifiedManifest[T <: Any : Manifest](value: T) {
val m = manifest[T] // So at this point we have the manifest for the Parameterized type
// At which point we could either do an if() expression on what type is contained in our manifest
if (m equals manifest[String]) {
println("The manifest contains a String")
} else if (m <:< manifest[AnyVal]) { // A subtype check using the <:< operation on the Manifest trait
println("The manifest contains a subtype of AnyVal")
} else if (m <:< manifest[AnyRef]) {
@kings13y
kings13y / PrintFormatter.scala
Created June 16, 2011 21:38
Generalized type constraints example
case class PrintFormatter[T](item : T) {
def formatString(implicit evidence: T =:= String) = { // Will only work for String PrintFormatters
println("STRING specialised printformatting...")
}
def formatPrimitive(implicit evidence: T <:< AnyVal) = { // Will only work for Primitive PrintFormatters
println("WRAPPED PRIMITIVE specialised printformatting...")
}
}
val stringPrintFormatter = PrintFormatter("String to format...")