diff --git a/2019/Scala/kamlib/Reader.scala b/2019/Scala/kamlib/Reader.scala new file mode 100644 index 0000000..320b9b1 --- /dev/null +++ b/2019/Scala/kamlib/Reader.scala @@ -0,0 +1,35 @@ +package kamlib + +import scala.io.Source + +/** File IO helper */ +object Reader { + + /** Read file with the name specified in parameter */ + def readList(filename: String): List[String] = + { + val src = Source.fromFile(getClass.getResource("/input1.txt").getFile) + val list = src.getLines().toList + src.close() + list + } + + /** Read file with the name specified in parameter */ + def readArray(filename: String): Array[String] = + { + val src = Source.fromFile(getClass.getResource("/input1.txt").getFile) + val array = src.getLines().toArray + src.close() + array + } + + /** Read file with the name specified in parameter */ + def readString(filename: String): String = + { + val src = Source.fromFile(getClass.getResource("/input1.txt").getFile) + val string = src.getLines().mkString + src.close() + string + } + +} diff --git a/2019/Scala/kamlib/Wrapper.scala b/2019/Scala/kamlib/Wrapper.scala new file mode 100644 index 0000000..601fb4a --- /dev/null +++ b/2019/Scala/kamlib/Wrapper.scala @@ -0,0 +1,29 @@ +package kamlib + +/** Wraps a function, measures the execution time, provides the result and time*/ +class Wrapper[A](function: => A) +{ + /** Saved results */ + val tuple: (A, Int) = wrap(function) + + /** Time measurement */ + private[this] def wrap[A](function: => A): (A, Int) = + { + val evalStart = System.currentTimeMillis() + val result = function + val evalEnd = System.currentTimeMillis() + (result, (evalEnd-evalStart).toInt) + } + + def print(): Unit = + { + println(s"Result: ${tuple._1} | Time: ${tuple._2}ms") + } + def result: A = tuple._1 + def time: Int = tuple._2 +} + +object Wrapper +{ + def apply[A](function: => A): Wrapper[A] = new Wrapper[A](function) +}