Helper library
This commit is contained in:
parent
84ee7b4e74
commit
adbd25aa77
35
2019/Scala/kamlib/Reader.scala
Normal file
35
2019/Scala/kamlib/Reader.scala
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
29
2019/Scala/kamlib/Wrapper.scala
Normal file
29
2019/Scala/kamlib/Wrapper.scala
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in a new issue