This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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._ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Repository[T] { | |
def withConnection(f: (Connection => Option[T])): Option[T] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env scala | |
!# | |
import io.Source | |
if(args.length < 1) { println("usage: ./scrabble <filename>"); System.exit(-1)} | |
Source.fromFile(args(0)).getLines.foreach(w => printf("%s,%d%n", w,score(w))) | |
def score(word:String) = word.foldLeft(0) (_ + scoreChar(_)) |
NewerOlder