Skip to content

Instantly share code, notes, and snippets.

@japaz
Created October 31, 2011 23:00
Show Gist options
  • Save japaz/1329328 to your computer and use it in GitHub Desktop.
Save japaz/1329328 to your computer and use it in GitHub Desktop.
7L7W Scala - Day 2
// Write a Censor trait with a method that will replace the curse words
// Shoot and Darn with Pucky and Beans alternatives. Use a map to
// store the curse words and their alternatives.
// Load the curse words and alternatives from a file.
import org.specs.runner.JUnit4
import org.specs.runner._
import org.specs._
import org.junit.runner.RunWith
import org.specs.runner.JUnitSuiteRunner
import scala.collection.mutable.HashSet
import scala.collection.mutable.LinkedList
import scala.collection.immutable.ListSet
@RunWith(classOf[JUnitSuiteRunner])
class CensorSpecTest extends Specification with JUnit {
"Censure of an empty string" should {
"return an empty string" in {
(new Censor {}).censure("") mustEqual "";
}
}
"Censure of an string with a censored word" should {
"return Pucky if the word is Shoot" in {
(new Censor {}).censure("Shoot") mustEqual "Pucky";
}
"return Beans if the word is Darn" in {
(new Censor {}).censure("Darn") mustEqual "Beans";
}
"replace all ocurrences of Shoot with Pucky" in {
(new Censor {}).censure("words Shoot Shoot other words") mustEqual "words Pucky Pucky other words";
}
"replace all ocurrences of Darn with Beans" in {
(new Censor {}).censure("words Darn Darn other words") mustEqual "words Beans Beans other words";
}
}
"Parsing of no lines" should {
val lines : List[String] = Nil
"return an empty map" in {
(new Censor {}).parseSubstitutions(lines.iterator) mustEqual Map();
}
}
"Parsing of lines" should {
"return a map with one pair if we have only one line" in {
val lines = List("Shoot Pucky")
(new Censor {}).parseSubstitutions(lines.iterator) mustEqual Map("Shoot" -> "Pucky");
}
"return a map with two pairs if we have two lines" in {
val lines = List("Shoot Pucky", "Darn Beans")
(new Censor {}).parseSubstitutions(lines.iterator) mustEqual Map("Shoot" -> "Pucky", "Darn" -> "Beans");
}
}
"Censure of an string with a censored word readed from a file" should {
val censor = (new Censor {})
censor.readSubstitutionsFromFile()
"return Pucky if the word is Shoot" in {
censor.censure("Shoot") mustEqual "Pucky";
}
"return Beans if the word is Darn" in {
censor.censure("Darn") mustEqual "Beans";
}
"replace all ocurrences of Shoot with Pucky" in {
censor.censure("words Shoot Shoot other words") mustEqual "words Pucky Pucky other words";
}
"replace all ocurrences of Darn with Beans" in {
censor.censure("words Darn Darn other words") mustEqual "words Beans Beans other words";
}
}
}
object CensorSpecMain {
def main(args: Array[String]) {
new CensorSpecTest().main(args)
}
}
trait Censor {
var substitutions = Map[String, String]("Shoot" -> "Pucky", "Darn" -> "Beans")
def censure(text : String) = {
if (text.length == 0)
""
else {
(text /: substitutions) {(censured, substitution) => censured.replaceAll(substitution._1, substitution._2)}
// var text2 = text
// substitutions.foreach ((substitution) => text2=text2.replaceAll(substitution._1, substitution._2))
// text2
}
}
def parseSubstitutions(lines : Iterator[String]) = {
substitutions = Map[String, String]()
lines.foreach ((line)=> {
val a = line.split("\\s")
substitutions = substitutions + (a.apply(0)->a.apply(1))
})
substitutions
}
def readSubstitutionsFromFile() = {
parseSubstitutions(scala.io.Source.fromFile("censor.txt").getLines)
}
}
Shoot Pucky
Darn Beans
// A discussion on how to use Scala files
// http://stackoverflow.com/questions/1284423/read-entire-file-in-scala
// http://www.naildrivin5.com/blog/2010/01/26/reading-a-file-in-scala-ruby-java.html
// What makes a closure different from a code block
// http://gleichmann.wordpress.com/2010/11/15/functional-scala-closures/
// Closures have free variables, which are not bounded
// Use fol dLeft to compute the total size of a list of strings
import org.specs.runner.JUnit4
import org.specs.runner._
import org.specs._
import org.junit.runner.RunWith
import org.specs.runner.JUnitSuiteRunner
import scala.collection.mutable.HashSet
import scala.collection.mutable.LinkedList
import scala.collection.immutable.ListSet
@RunWith(classOf[JUnitSuiteRunner])
class SizerSpecTest extends Specification with JUnit {
"An empty list" should {
val elements : List[String] = Nil;
"have total size 0" in {
(new Sizer(elements)).totalSize() mustEqual 0;
}
}
"A list with one String" should {
val elements = List("1234")
"have total size equals to the size of the String" in {
(new Sizer(elements)).totalSize() mustEqual 4;
}
}
"A list with one Empty String" should {
val elements = List("")
"have total size equals to " in {
(new Sizer(elements)).totalSize() mustEqual 0;
}
}
"A list with two Strings" should {
val elements = List("1234", "5678")
"have total size equals to the sum of the size of the Strings" in {
(new Sizer(elements)).totalSize() mustEqual 8;
}
}
}
object SizerSpecMain {
def main(args: Array[String]) {
new SizerSpecTest().main(args)
}
}
class Sizer(elements : List[String]) {
def totalSize() = (0 /: elements) {_+_.size}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment