Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import ChecksumAccumulator.calculate
  2.  
  3. object Summer{
  4. def main(args: Array[String]): Unit= {
  5.  
  6. for (arg <- args)
  7. println(arg + " : " + calculate(arg))
  8.  
  9.  
  10. }
  11.  
  12. }
  13.  
  14. object ChecksumAccumulator {
  15.  
  16.  
  17. /*
  18. When a singleton object shares the same name
  19. with a class, it is called that class's companion object.
  20. */
  21.  
  22. private val cache = mutable.Map.empty[String, Int]
  23.  
  24.  
  25. def calculate(s: String): Int =
  26.  
  27. if (cache.contains(s))
  28. cache(s)
  29. else {
  30. val acc = new ChecksumAccumulator
  31.  
  32. //The object with the same name of the class can acces to the private members.
  33. //println(acc.sum)
  34. for (c <- s)
  35. acc.add(c.toByte)
  36. val cs = acc.checksum()
  37. cache += (s -> cs)
  38. cs
  39. }
  40. def showMap() : Unit = {
  41. for(base:String <- cache.keys)
  42. println(cache(base))
  43.  
  44. }
  45. //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