Skip to content

Instantly share code, notes, and snippets.

Kingsley Davies kings13y

Block or report user

Report or block kings13y

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@kings13y
kings13y / FizzBuzz.scala
Created May 28, 2012
Minimal FizzBuzz implementation in Scala
View FizzBuzz.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 / MinimalSoapServer.scala
Created Feb 2, 2011
Minimal Soap Server using Scala and JDK6 annotations
View MinimalSoapServer.scala
import javax.jws.WebService
import javax.jws.soap.SOAPBinding
import javax.jws.soap.SOAPBinding.Style
import javax.xml.ws.Endpoint
@WebService(targetNamespace="org.scalabound.test", name="org.scalabound.test", portName="test", serviceName="WSTest")
private class MinimalSoapServer {
@SOAPBinding(style = Style.RPC)
def test(value : String) = "Hi " + value
@kings13y
kings13y / BowlingKataRunner.scala
Created Mar 23, 2011
Sample solution to standard bowling kata (http://codingdojo.org/cgi-bin/wiki.pl?KataBowling) using Scala pattern matching and extractors
View BowlingKataRunner.scala
// Idiomatic solution using extrators
// Extractors allow us to bind parameters to an object and are usable in pattern matches to see if a pattern can apply to the params submitted.
// Note this is simialr to case classes, but works against an Object (which are like 'static' classes in Java). Also it incurs some performance penalties
// as opposed to using case classes
class BowlingForExtractors() {
def score(bowls:List[Int]) = scoreFrames(bowls).slice(0,10).sum // take the first 10 elements from the List and sum them..any additional elements are the result of additional strikes
def scoreFrames(bowls:List[Int]) : List[Int] = bowls match {
case Nil => List(0)
@kings13y
kings13y / FizzBuzzScalaTest.scala
Created May 28, 2012
FizzBuzz Test spec with ScalaTest
View FizzBuzzScalaTest.scala
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 / FizzBuzzJUnitTest.java
Created May 28, 2012
FizzBuzz Java JUnit test
View FizzBuzzJUnitTest.java
package org.scalabound.scatdd;
import static org.junit.Assert.*;
import org.junit.Test;
import org.scalabound.scatdd.FizzBuzz;
public class FizzBuzzJUnitTest {
@kings13y
kings13y / FizzBuzzSpecs2.scala
Created May 28, 2012
FizzBuzz test case in Specs2
View FizzBuzzSpecs2.scala
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 / FizzBuzzScalaCheck.scala
Created May 28, 2012
FizzBuzz spec using ScalaCheck
View FizzBuzzScalaCheck.scala
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 / gist:2136456
Created Mar 20, 2012
mvn archetype quickstart commands for Scala
View gist:2136456
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 Aug 9, 2011
Sample Rest style service using just Scala and Java 6 annotations
View MinimalRestServer.scala
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 Jul 26, 2011
Partial updates with immutable domain objects
View PartialUpdateToImmutableObject.scala
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")
You can’t perform that action at this time.