Advertisement
obernardovieira

Clipping Bushes And Other New Tricks (day 2) [SLiSW]

Aug 12th, 2015
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.86 KB | None | 0 0
  1. //exercise 1
  2.  
  3. val myList = List("abc","ef","ghij")
  4. myList.foldLeft(0)((sum, value) => sum + value.length)
  5.  
  6. //exercise 2
  7.  
  8. import scala.io.Source
  9. import scala.collection.mutable.HashMap
  10.  
  11. class Censor {
  12.     var theMap = new HashMap[String, String]
  13.  
  14.     /*in file myfile.txt
  15.  
  16.     Shoot,Pucky
  17.     Darn,Beans
  18.  
  19.     */
  20.  
  21.     for(line <- Source.fromFile("myfile.txt").getLines()) {
  22.         var parts = line.split(",")
  23.         theMap += parts(0) -> parts(1)
  24.     }
  25.  
  26.     def changeWords(thePhrase : String) = theMap.foldLeft(thePhrase)((output, input) => output.replaceAll(input._1, input._2))
  27.  
  28. }
  29.  
  30. class theText(txt: String) extends Censor {
  31.     def original = txt
  32.  
  33.     def changed = changeWords(txt)
  34. }
  35.  
  36.  
  37. val text = new theText("Shoot, I forgot my Darn traits again")
  38. println("Original String: " + text.original)
  39. println("Censored String: " + text.changed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement