Skip to content

Instantly share code, notes, and snippets.

View p3t0r's full-sized avatar

Peter Maas p3t0r

  • Adevinta
  • Hilversum, the Netherlands
View GitHub Profile
@p3t0r
p3t0r / unary oparator.scala
Created January 29, 2009 19:09
overloading a unary operator in scala
class MyString(string:String){
var value = string
def unary_- = new MyString(new StringBuilder(value).reverse.toString)
override def toString = value
}
val myString = new MyString("abcd")
println(myString) // prints "abcd"
println(-myString) // prints "dcba"
@p3t0r
p3t0r / Scorer.scala
Created June 30, 2011 08:37
Scala TDD workshop - Implement a Yahtzee scorer
/**
* Results of a 1 hour 'bucketline' exercise @ Marktplaats to test-drive the development
* of a Yahtzee scorer in Scala.
*/
// --- the test --- //
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import Scorer._
package com.log4.methodmissing
import java.io.File
class Path(name: String = "", parent: Option[Path] = None) extends Dynamic {
def applyDynamic(methodName: String)(args: Any*) = new Path(methodName, Option(this))
override def toString = (if(parent.isDefined) parent.get.toString + File.separatorChar else "") + name
}
@p3t0r
p3t0r / ParameterInMethodNameExample.scala
Created April 28, 2011 19:25
example of encoding parameters in the methodname using the Dynamic trait
package com.log4.methodmissing
import java.lang.Integer
class ParameterInMethodNameExample extends Dynamic {
def find(by:String, value:String) = "select from users where %s = '%s'".format(by, value)
def find(by:String, value:Integer) = "select from users where %s = %s".format(by, value)
def delete(by:String, value:String) = "delete from users where %s = '%s'".format(by, value)
def delete(by:String, value:Integer) = "delete from users where %s = %s".format(by, value)
trait Retrying[T] extends Repository[T] {
abstract override def withConnection(f: (Connection => Option[T])): Option[T] = retry(f)
private def retry(f: (Connection => Option[T]), times: Int = 3): Option[T] = {
try {
println("number of tries left: %d".format(times))
super.withConnection(f)
} catch {
case _ if times == 0 => None
case _ => retry(f, times - 1)
class BigFailRepository extends Repository[String] {
override def withConnection(f: (Connection => Option[String])): Option[String] = {
throw new UnsupportedOperationException("BAM")
}
}
class SmallFailRepository extends Repository[String] {
var numFails = 0
override def withConnection(f: (Connection => Option[String])): Option[String] = {
val bfr = new BigFailRepository with Retrying[String]
println("failed result: " + bfr.withConnection {conn => Option("failure result")})
val sfr = new SmallFailRepository with Retrying[String]
println("success result: " + sfr.withConnection {conn => Option("should fail first, succeed later")})
val nfr = new NoFailRepository with Retrying[String]
println("success result: " + nfr.withConnection {conn => Option("should not fail result")})
abstract class Repository[T] {
def withConnection(f: (Connection => Option[T])): Option[T]
}
package com.log4p;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Scorer {
public static void main(String args[]) {
FileInputStream fis = null;
package com.log4p;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Scorer {