scala day 2

This commit is contained in:
Max Hohlfeld 2023-05-12 12:42:18 +02:00
parent 97920a52fe
commit 0a92c9cec9
2 changed files with 45 additions and 0 deletions

1
scala/corrections.txt Normal file
View File

@ -0,0 +1 @@
blub#gluck

44
scala/day2.scala Normal file
View File

@ -0,0 +1,44 @@
import scala.io.Source
def getTotalSize(strings: List[String]): Int = {
(0 /: strings) {(sum, string) => sum + string.size}
}
val list = List("123", "345", "ABN")
val bla = getTotalSize(list)
println(bla)
trait Censor {
var corrections = Map("Shoot" -> "Pucky", "Darn" -> "Beans")
def readCorrections() = {
val filename = "corrections.txt"
for (line <- Source.fromFile(filename).getLines) {
val splitted = line.split("#")
corrections = corrections + (splitted(0) -> splitted(1))
}
}
def correct(input: String): String = {
var output = input;
corrections.foreach((e: (String, String)) => output = e._1.r.replaceAllIn(output, e._2))
return output
}
}
class Corrector extends Censor {
def say(input: String) = {
val output = correct(input)
println(output)
}
}
val hering = new Corrector()
hering.readCorrections()
hering.say("I'm a fish, blub blub")
hering.say("Shoot Darn")