From f73f01f616a0cf273db2ac2151a4a0977c04c306 Mon Sep 17 00:00:00 2001 From: Jason Staten Date: Sun, 10 Nov 2019 19:25:52 -0700 Subject: [PATCH] day2 --- .../main/resources/alternatives.properties | 2 + scala/src/main/scala/day2/Censor.scala | 43 +++++++++++++++++++ scala/src/main/scala/day2/FullList.scala | 17 ++++++++ 3 files changed, 62 insertions(+) create mode 100644 scala/src/main/resources/alternatives.properties create mode 100644 scala/src/main/scala/day2/Censor.scala create mode 100644 scala/src/main/scala/day2/FullList.scala diff --git a/scala/src/main/resources/alternatives.properties b/scala/src/main/resources/alternatives.properties new file mode 100644 index 0000000..5bc583f --- /dev/null +++ b/scala/src/main/resources/alternatives.properties @@ -0,0 +1,2 @@ +stupid=jolly +vermin=furball \ No newline at end of file diff --git a/scala/src/main/scala/day2/Censor.scala b/scala/src/main/scala/day2/Censor.scala new file mode 100644 index 0000000..2c4ab6b --- /dev/null +++ b/scala/src/main/scala/day2/Censor.scala @@ -0,0 +1,43 @@ +package day2 +import java.util.Properties +import scala.collection.JavaConverters._ +import scala.io.Source +import scala.util.Using + +trait Censor { + val substitutions : Map[String, String] + val source : String + override def toString(): String = { + source + .split(" ") + .map((word) => substitutions.getOrElse(word, word)) + .mkString(" ") + } +} + +object CensorRun extends App { + val chars = new Censor { + val substitutions = Map( + "shoot" -> "pucky", + "darn" -> "beans", + ) + val source = "shoot that darn truck" + } + println(chars) +} + +object CensorFromFile extends App { + val loadedSubstitutions = Using.Manager {use => + val props = new Properties() + val input = use(Source.fromResource("alternatives.properties").reader()) + props.load(input) + + props.asScala.toMap + }.getOrElse(Map()) + + val chars = new Censor { + val substitutions = loadedSubstitutions + val source = "That stupid vermin did it again" + } + println(chars) +} \ No newline at end of file diff --git a/scala/src/main/scala/day2/FullList.scala b/scala/src/main/scala/day2/FullList.scala new file mode 100644 index 0000000..6252717 --- /dev/null +++ b/scala/src/main/scala/day2/FullList.scala @@ -0,0 +1,17 @@ +package day2 + +object FullList extends App { + + val strings = List( + "Earth", + "Fire", + "Wind", + "Water", + "Heart" + ) + + val total = strings.map(_.length()).foldLeft(0)((acc, len) => acc + len) + + println(total) + +} \ No newline at end of file