Aula 11 Scala

Scala online

https://learnxinyminutes.com/docs/scala/

https://docs.scala-lang.org

compila p/ JVM - permite rodar bibliotecas compiladas de Java

acoplado a um ambiente de execucão distribuida Apache Spark https://spark.apache.org

algumas diferenças entre Scala 2 e 3 (como Python 2 e 3)

val addOne = (x: Int) => x + 1

metodo

def addOne(x:Int) : Int => {
  ...;
  ...;
  x+1
  }
class Point(var x: Int, var y: Int):

  def move(dx: Int, dy: Int): Unit =
    x = x + dx
    y = y + dy

  override def toString: String =
    s"($x, $y)"
end Point

parametros do construtor na definicao da classe

paramentros do construtor sao automaticamente atributos

override explicito

val twentySomethings =
  for user <- userBase if user.age >=20 && user.age < 30
  yield user.name 
val salaries = Seq(20_000, 70_000, 40_000)
val newSalaries = salaries.map(x => x * 2)
  
val a = List("jane", "jon", "mary", "joe")
val b = a.filter(_.startsWith("j"))
         .map(_.capitalize)  
  
def promotion(salaries: List[Double], promotionFunction: Double => Double): List[Double] =
    salaries.map(promotionFunction)  
  
def makeInt(s: String): Option[Int] =
  try
    Some(Integer.parseInt(s.trim))
  catch
    case e: Exception => None

val stringA = "one"
val stringB = "2"
val stringC = "3"

val y = for
  a <- makeInt(stringA)
  b <- makeInt(stringB)
  c <- makeInt(stringC)
yield
  a + b + c