Advertisement
Guest User

scala questions

a guest
Dec 1st, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.14 KB | None | 0 0
  1.  
  2. 1 => Is there an efficient patern that forces you to implement wrapper class and matcher.
  3. When you store most class atributes but have to implement runtime atributes as well.
  4.  
  5. case class AnimalAtributeWrapper(size:Int,color:Color) loaded or recieved from external source
  6. case class AnimalImp(size:Int,color:Color,runtimeAtr:Anyref)
  7.  
  8. runtime atribute could be (x:Int,y:Int) coordinate generated by the program or OS specific implemantation of animal.
  9.  
  10. Core problem Wrapper -> matcher -> Implemantation, for every new Implementation I have to define wraper and match it to Implemantation.
  11.  
  12.  
  13. 2 => When is it worth it to use lazy vals.
  14.  
  15. case class cat (val size:Int, var age:String) {
  16.  
  17. //Good if you dont want value to change based on depenecies
  18. val value:String = s"Original cat atributes: $size,$age"
  19.  
  20. //Good if dependencies change
  21. def method() = s"Current cat atributes: $size,$age"
  22.  
  23. //Good if dependencies are immutable?
  24. //Size never changes so the cast can be done whenever this value is requested
  25. lazy val sizeTokillorams:Int = size * 10
  26. }
  27.  
  28.  
  29. 3 =>  Flyweight design pattern.
  30. Why not use this for all immutable classes? Memmory leak with infinite object posiibilities?
  31. Is it possible to find if object reference is used somewhere else (Do manual garbage collect)?
  32.  
  33. 4) Difference between Maps:
  34. If you pass immutable map ref it will never change wile mutable will mutate (shared state)
  35.  
  36.  
  37. //When you pass it around a lot?
  38. import scala.collection.immutable.Map
  39. var mapIm = Map()
  40. mapIm += (key -> value)
  41.  
  42. //When private?
  43. import scala.collection.mutable.Map
  44. val mapMu = Map.empty[String, Tile]
  45. mapMu.update(key,value)
  46.  
  47. 4 => Defoult parameter value based on other parameters.
  48.  
  49. trait Animal {
  50. def size():Float
  51. }
  52.  
  53. class Cat(val name:String, val size:Float) extends Animal {
  54.     def this(name) = this(name,Cat.genSize(name)
  55.  
  56.     /*
  57.     Cant do this "aplication does not take paremeters compiler error"
  58.     def this(name) ={
  59.         val size = 5f //cant initalize an intermidiate val
  60.         this(name,size)
  61.     }
  62.     */
  63. }
  64.  
  65. object Cat {
  66. def genSize(name):Flaot = RandomFloat(seed = name,from = 1f,to = 10f)
  67. }
  68.  
  69. 6) Testing private methods of an abstract class.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement