Helper library

This commit is contained in:
kamoshi 2019-12-06 22:10:37 +01:00 committed by GitHub
parent 84ee7b4e74
commit adbd25aa77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 0 deletions

View 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
}
}

View 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)
}