Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ChecksumAccumulator.calculate
- object Summer{
- def main(args: Array[String]): Unit= {
- for (arg <- args)
- println(arg + " : " + calculate(arg))
- }
- }
- object ChecksumAccumulator {
- /*
- When a singleton object shares the same name
- with a class, it is called that class's companion object.
- */
- private val cache = mutable.Map.empty[String, Int]
- def calculate(s: String): Int =
- if (cache.contains(s))
- cache(s)
- else {
- val acc = new ChecksumAccumulator
- //The object with the same name of the class can acces to the private members.
- //println(acc.sum)
- for (c <- s)
- acc.add(c.toByte)
- val cs = acc.checksum()
- cache += (s -> cs)
- cs
- }
- def showMap() : Unit = {
- for(base:String <- cache.keys)
- println(cache(base))
- }
- //def showMap(): String = { var cadena = cache("Every value is an object.") + " "+ cache("Every value is an object. per second time!!"); cadena }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement