SlideShare una empresa de Scribd logo
1 de 61
Scala

Uma poderosa linguagem para a JVM




                     Isaías Cristiano Barroso
                         Strategia Tecnologia

                         29 de Maio de 2012
 Criador da linguagem Scala

 Compilador de Referência JAVA

 Co-autor Java Generics



                                            Martin Odersky
                               EPFL - École polytechnique fédérale de Lausanne
Scala – O que disse James Gosling?




        "If I were to pick a language
      to use today other than Java,
                   it would be Scala."
                                     -- James Gosling, criador do Java
Scala – Visão Geral




   Compatibilidade com a JVM (JDK 1.5+)

   Linguagem funcional

   Empresa Typesafe (www.typesafe.com) mantem
  junto a comunidade a linguagem Scala.
Scala – Comparação de Byte Code

 public class Hello {                       class Hello {
     public String sayHello(String name){       def sayHello(name: String):String = {
         return "Hello " + name;                    "Hello " + name
     }                                          }
 }                                          }




                 Código JAVA vs Scala
Scala – Comparação de Byte Code

 public class Hello extends                    public class Hello extends Object
 java.lang.Object{                             implements scala.ScalaObject{
 public Hello();                               public java.lang.String
   Code:                                       sayHello(java.lang.String);
    0: aload_0                                   Code:
    1: invokespecial #1; //Method                 0: new     #7; //class
 java/lang/Object."<init>":()V                 scala/collection/mutable/StringBuilder
    4: return                                     3: dup
                                                  4: invokespecial #11; //Method
 public java.lang.String                       scala/collection/mutable/StringBuilder."<i
 sayHello(java.lang.String);                   nit>":()V
   Code:                                          7: ldc     #14; //String Hello
    0: new     #2; //class                        9: invokevirtual #18; //Method
 java/lang/StringBuilder                       scala/collection/mutable/StringBuilder.app
    3: dup                                     end:(Ljava/lang/Object;)Lscala/collection/
    4: invokespecial #3; //Method              mutable/StringBuilder;
 java/lang/StringBuilder."<init>":()V             12: aload_1
    7: ldc     #4; //String Hello                 13: invokevirtual #18; //Method
    9: invokevirtual #5; //Method              scala/collection/mutable/StringBuilder.app
 java/lang/StringBuilder.append:(Ljava/lang/   end:(Ljava/lang/Object;)Lscala/collection/
 String;)Ljava/lang/StringBuilder;             mutable/StringBuilder;
    12: aload_1                                   16: invokevirtual #22; //Method
    13: invokevirtual #5; //Method             scala/collection/mutable/StringBuilder.toS
 java/lang/StringBuilder.append:(Ljava/lang/   tring:()Ljava/lang/String;
 String;)Ljava/lang/StringBuilder;                19: areturn
    16: invokevirtual #6; //Method             public Hello();
 java/lang/StringBuilder.toString:               Code:
 ()Ljava/lang/String;                             0: aload_0
    19: areturn                                   1: invokespecial #30; //Method
                                               java/lang/Object."<init>":()V
 }                                                4: return
                                               }
Scala – Orientada a Objetos

 Cada valor é um objeto
Scala – Orientada a Objetos

 Cada valor é um objeto

 scala> val a = 10
 a: Int = 10

 scala> a.
 %                   &              *              +         -

 /                   >              >=             >>        >>>

 ^                   asInstanceOf   isInstanceOf   toByte    toChar

 toDouble            toFloat        toInt          toLong    toShort

 toString            unary_+        unary_-        unary_~   |

 scala> a.+(10)
 res0: Int = 20

 scala> a + 10
 res1: Int = 20
Scala – Orientada a Objetos

 Classes

 class Point(xc: Int, yc: Int) {
   var x: Int = xc
   var y: Int = yc
   def move(dx: Int, dy: Int) {
     x = x + dx
     y = y + dy
   }
   override def toString(): String = "(" + x + ", " + y + ")";
 }

 scala> val p = new Point(10, 20)
 p: Point = (10, 20)

 scala> p
 res0: Point = (10, 20)
Scala – Orientada a Objetos

 Classes (Scala vs JAVA)
 class Point(xc: Int, yc: Int) {
   var x: Int = xc
   var y: Int = yc
   def move(dx: Int, dy: Int) {
     x = x + dx
     y = y + dy
   }
   override def toString(): String = "(" + x + ", " + y + ")";
 }
 public class Point {
      int x;
      int y;
      Point(int xc, int yc){
            x = xc;
            y = yc;
      }
      public void move(int dx, int dy){
            x = x + dx;
            y = y + dy;
      }
      public String toString() {
            return "(" + x + ", " + y + " )";
      }
 }
Scala – Orientada a Objetos


 Traits
 
  Utilizadas para definir tipos e métodos suportados de um
 objeto
 
   Similar as interfaces JAVA
 
   Podem ser parcialmente implementadas
 
     Não tem parâmetros para construtores
Scala – Orientada a Objetos


 Traits
 trait Similarity {
   def isSimilar(x: Any): Boolean
   def isNotSimilar(x: Any): Boolean = !isSimilar(x)
 }

 class Point(xc: Int, yc: Int) extends Similarity {
   var x: Int = xc
   var y: Int = yc
   def isSimilar(obj: Any) =
     obj.isInstanceOf[Point] &&
     obj.asInstanceOf[Point].x == x
 }

 object TraitsTest extends Application {
   val p1 = new Point(2, 3)
   val p2 = new Point(2, 4)
   val p3 = new Point(3, 3)
   println(p1.isNotSimilar(p2)) // false
   println(p1.isNotSimilar(p3)) // true
   println(p1.isNotSimilar(2)) // true
 }
Scala – Orientada a Objetos

 “object”
 
     Equivalente a uma Singleton class to Java



 …

 object TraitsTest extends Application {
   val p1 = new Point(2, 3)
   val p2 = new Point(2, 4)
   val p3 = new Point(3, 3)
   println(p1.isNotSimilar(p2)) // false
   println(p1.isNotSimilar(p3)) // true
   println(p1.isNotSimilar(2)) // true
 }
Scala – Orientada a Objetos

 “Companion Object”
 
  Mesmo nome da classe
 
  Definido no mesmo arquivo da classe
 
  Possui acesso a todos os membros da classe
 
  Muito utilizado como Factory




     http://www.scala-lang.org/api
Scala – Orientada a Objetos

 “Companion Object”




    http://www.scala-lang.org/api
Scala – Orientada a Objetos

 “object”

 scala> val bd = BigDecimal("123.43")
 bd: scala.math.BigDecimal = 123.43

 scala> val bd = BigDecimal(123.43)
 bd: scala.math.BigDecimal = 123.43


 “apply Method”

 scala> val bd = BigDecimal.apply("123.43")
 bd: scala.math.BigDecimal = 123.43

 scala> val bd = BigDecimal.apply(123.43)
 bd: scala.math.BigDecimal = 123.43
“Case Class”
Scala – Orientada a Objetos


 
     Classes Scala como qualquer outra
 
     Exportam seus parâmetros de construtores
 
     Método apply criado automaticamente
 
  Permite decomposição funcional através de “pattern
 matching”
Scala – Orientada a Objetos


 “Case Class”
 scala> abstract class Pessoa
 defined class Pessoa

 scala> case class PessoaFisica(nome: String, idade: Int) extends
 Pessoa
 defined class PessoaFisica

 scala> case class PessoaJuricia(nome: String, tempoAbertura: Int)
 extends Pessoa
 defined class PessoaJuricia
 toString implementado automaticamente
 scala> val pf = PessoaFisica("Joao", 83)
 pf: PessoaFisica = PessoaFisica(Joao,83)

 scala> val pj = PessoaJuridica("The Corp", 4)
 pj: PessoaJuridica = PessoaJuridica(The Corp,4)

 scala> println(pf)
 PessoaFisica(Joao,83)

 scala> println(pj)
 PessoaJuridica(The Corp,4)
Scala – Orientada a Objetos


 “Case Class”
 equals implementado automaticamente
 scala> val x = PessoaFisica("Joao", 83)
 x: PessoaFisica = PessoaFisica(Joao,83)

 scala> val y = PessoaFisica("Joao", 83)
 y: PessoaFisica = PessoaFisica(Joao,83)

 scala> val z = PessoaFisica("Maria", 78)
 z: PessoaFisica = PessoaFisica(Maria,78)

 scala> x == y
 res3: Boolean = true

 scala> x == z
 res4: Boolean = false

 apply de case class em ação
 scala> val pf = PessoaFisica("Isaias")
 <console>:9: error: not enough arguments for method apply: (nome:
 String, idade: Int)PessoaFisica in object PessoaFisica.
 Unspecified value parameter idade.
        val pf = PessoaFisica("Isaias")
Scala – Orientada a Objetos


 “Pattern Matching”

      def testPM(p: Pessoa){
          p match {
              case PessoaFisica(nome, idade) =>
                  println("Nome: " + nome)
                  println("Idade: " + idade)
              case PessoaJuridica(nome, tempo) =>
                  println("Nome: " + nome)
                  println("Tempo: " + tempo)
              case _ =>
                  println("Tipo Nao Esperado")
          }
      }



 “Sealed Case Class”
 scala> sealed abstract class Pessoa
 defined class Pessoa
Scala – Linguagem Funcional

 
   High Order Functions (Funções podem receber e
 retornar funções)
 
     Programação concorrente
 
     Encoraja a utilização de variáveis imutáveis (var x val)
 
     Estaticamente tipada
Scala – Linguagem Funcional


  Fatorial
 def fac(n : Int) = {
      var r = 1
      for (i <- 1 to n) r = r * i
            r
 }

 scala> fac(5)
 res0: Int = 120

  Fatorial - Recursivo
 def fac(n: Int): Int =
      if (n <= 0) 1
            else                       Não
      n * fac(n – 1)                Otimizado
 scala> fac(5)
 res0: Int = 120
Scala – Linguagem Funcional

  @tailrec
 def fac(n: Int): Int =
      if (n <= 0) 1
            else
      n * fac(n – 1)
 <console>:10: error: could not optimize @tailrec annotated method
 fac: it contains a recursive call not in tail position
        if (n <= 0) 1
                 else
             n * fac(n -1)
                 ^

    Fatorial – Tail Recursive

 @tailrec def factorial(accumulator: Int, number: Int) : Int = {
       if(number == 1)
         return accumulator
       factorial(number * accumulator, number - 1)
     }                                                     Otimizado
Scala – Linguagem Funcional

  Melhorando a função
 def factorial(number: Int) : Int = {
     def factorialWithAccumulator(accumulator: Int, number: Int) : Int
 = {
         if (number == 1)
              return accumulator
         else
              factorialWithAccumulator(accumulator * number, number - 1)
     }
     factorialWithAccumulator(1, number)
 }
Scala – Linguagem Funcional

 Functions
 def <nome>(parametros) : <retorno>
 def funcao1() {
    println(“Funcao sem parametro e sem retorno”)
 }

 def funcao2(x: Int, y: String) {
    println(“Funcao com parametro e sem retorno”)
 }

 def funcao3(x: Int, y: String) = {
      println(“Funcao com parametros e retorno implicito”)
    “retorno”
 }

 def funcao4(x: Int, y: String):String = {
      println(“Funcao com parametros e retorno explicito”)
    “retorno”
 }
Scala – Linguagem Funcional

 Funções Anônimas
 scala> (x: Int) => x + 1
 res0: Int => Int = <function1>

 scala> res0(1)
 res1: Int = 2



 Funções como valores
 scala> val inc = (x: Int) => x + 1
 inc: Int => Int = <function1>

 scala> inc(10)
 res0: Int = 11
Scala – Linguagem Funcional


 High Order Functions
 scala> def soma(x:Int, y:Int):Int = {
      | x+y
      | }
 soma: (x: Int, y: Int)Int

 scala> soma(10,20)
 res5: Int = 30

 scala> def withLog(f:(Int, Int) => Int, x:Int, y:Int):Int = {
      | println("Iniciando execucao da funcao")
      | val result = f(x,y)
      | println("Finalizando Execucao")
      | result
      | }
 withLog: (f: (Int, Int) => Int, x: Int, y: Int)Int

 scala> withLog(soma, 10, 20)
 Iniciando execucao da funcao
 Finalizando Execucao
 res6: Int = 30
Scala – Linguagem Funcional

     Querulous
 queryEvaluator.transaction { transaction =>
    transaction.select("SELECT ... FOR UPDATE", ...)
    transaction.execute("INSERT INTO users VALUES (?, ?)", 1,
 "Jacques")
    transaction.execute("INSERT INTO users VALUES (?, ?)", 2, "Luc")
 }
 //
 def transaction[T](f: Transaction => T) = {
      withTransaction { transaction =>
        transaction.begin()
        try {
          val rv = f(transaction)
          transaction.commit()
          rv
        } catch {
          case e: Throwable =>
             transaction.rollback()
             throw e
        }
      }
    }
    private def withTransaction[A](f: Transaction => A) = {
      database.withConnection { connection => f(new
 Transaction(queryFactory, connection)) }
    }
Scala – Linguagem Funcional


 Implicit Parameters
 scala> def addOne(implicit x:Int) = {
      | x+1
      | }
 addOne: (implicit x: Int)Int


 scala> addOne
 <console>:9: error: could not find implicit value for parameter x:
 Int
               addOne
               ^

 scala> implicit val x = 10
 x: Int = 10

 scala> addOne
 res1: Int = 11

 scala> addOne(35)
 res2: Int = 36
Scala – Linguagem Funcional


 Implicit Conversions
 scala> class RichString(s:String)
 defined class RichString

 scala> implicit def string2RichString(s:String) = new RichString(s)
 string2RichString: (s: String)RichString

 scala> val x = "Isaias"
 x: java.lang.String = Isaias

 scala> def printString(r: RichString) {
      | println(r)
      | }
 printString: (r: RichString)Unit


 scala> printString(x)
 $line1.$read$$iw$$iw$RichString@6fafc4c2

 scala>
Scala – Linguagem Funcional


 Mixin Class Composition
 abstract class AbsIterator {
   type T
   def hasNext: Boolean
   def next: T
 }

 trait RichIterator extends AbsIterator {
   def foreach(f: T => Unit) { while (hasNext) f(next) }
 }

 class StringIterator(s: String) extends AbsIterator {
   type T = Char
   private var i = 0
   def hasNext = i < s.length()
   def next = { val ch = s charAt i; i += 1; ch }
 }

 object StringIteratorTest {
   def main(args: Array[String]) {
     class Iter extends StringIterator(args(0)) with RichIterator
     val iter = new Iter
     iter foreach println
   }
 }
Scala – Collections


 Lists
 val numbers = List(1, 2, 3, 4)
 numbers: List[Int] = List(1, 2, 3, 4)



 Sets
 scala> Set(1, 1, 2)
 res0: scala.collection.immutable.Set[Int] = Set(1, 2)
Scala – Collections


 Tuplas
 scala> val hostPort = ("localhost", 80)
 hostPort: (String, Int) = (localhost, 80)

 scala> hostPort._1
 res5: java.lang.String = localhost

 scala> hostPort._2
 res6: Int = 80



 Tuplas – Pattern Matching
 hostPort match {
   case ("localhost", port) => ...
   case (host, port) => ...
 }
Scala – Collections


 Map
 Map(1 -> 2)
 res0: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)

 Map("foo" -> "bar")
 res0:
 scala.collection.immutable.Map[java.lang.String,java.lang.String] =
 Map(foo -> bar)



 Map(1 -> Map("foo" -> "bar"))
 res0:
 scala.collection.immutable.Map[Int,scala.collection.immutable.Map[jav
 a.lang.String,java.lang.String]] = Map(1 -> Map(foo -> bar))
Scala – Collections


 Option
 val map = Map("Hi" -> "Dan", "Hello" -> "Jane")
 val result = map.get( "Hello" )

 result match {
   case None => println("No value found for key!")
   case Some(x) => print("Found value" + x)
 }



 scala> map.get("NoKey")
 res5: Option[java.lang.String] = None
Scala – Collections

 Functional Combinators
     “A saída de uma função será utilizada como entrada para outra
       função”
 scala> val numbers = List(1, 2, 3, 4)
 numbers: List[Int] = List(1, 2, 3, 4)

 scala> numbers.map((i:Int) => i * 2)
 res0: List[Int] = List(2, 4, 6, 8)

 scala> numbers.map(_ * 2)
 res1: List[Int] = List(2, 4, 6, 8)


 scala> numbers.foreach((i: Int) => i * 2) // Sem retorno

 scala> var value = 0
 value: Int = 0

 scala> numbers.foreach((i: Int) => value = i*2)

 scala> value
 res1: Int = 28
Scala – Collections


 Functional Combinators
 scala> numbers.filter((i: Int) => i%2==0)
 res0: List[Int] = List(2, 4)

 scala> def isEven(i: Int): Boolean = i % 2 == 0
 isEven: (i: Int)Boolean

 scala> numbers.filter(isEven _)
 res2: List[Int] = List(2, 4)




 scala> List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).partition((i: Int) => i >
 5)
 res0: (List[Int], List[Int]) = (List(6, 7, 8, 9, 10),List(1, 2, 3, 4,
 5))

 scala> res0._1
 res1: List[Int] = List(6, 7, 8, 9, 10)

 scala> res0._2
 res2: List[Int] = List(1, 2, 3, 4, 5)
Scala – Collections


 Functional Combinators
 scala> val numbers = List(1,2,3,4,5,6,7,8,9,10)
 numbers: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

 scala> numbers.find((i: Int) => i > 5)
 res5: Option[Int] = Some(6)




 scala> numbers.drop(5)
 res0: List[Int] = List(6, 7, 8, 9, 10)

 scala> numbers.dropWhile((i: Int) => i < 6)
 res0: List[Int] = List(6, 7, 8, 9, 10)
Scala – Collections


 Functional Combinators
 scala> numbers.foldLeft(0)((m: Int, n: Int) => m + n)
 res0: Int = 55



 scala> numbers.foldLeft(0) { (m: Int, n: Int) => println("m: " + m +
 " n: " + n); m + n }
 m: 0 n: 1
 m: 1 n: 2
 m: 3 n: 3
 m: 6 n: 4
 m: 10 n: 5
 m: 15 n: 6
 m: 21 n: 7
 m: 28 n: 8
 m: 36 n: 9
 m: 45 n: 10
 res0: Int = 55
Scala – Collections


 Functional Combinators
 scala> numbers.foldRight(0)((m: Int, n: Int) => m + n)
 res0: Int = 55



 scala> numbers.foldRight(0)((m: Int, n: Int) => println("m: " + m + "
 n: " + n); m + n)
 m: 10 n: 0
 m: 9 n: 10
 m: 8 n: 19
 m: 7 n: 27
 m: 6 n: 34
 m: 5 n: 40
 m: 4 n: 45
 m: 3 n: 49
 m: 2 n: 52
 m: 1 n: 54
 res0: Int = 55
Scala – Collections


 Functional Combinators
 scala> List(List(1, 2), List(3, 4)).flatten
 res0: List[Int] = List(1, 2, 3, 4)



 scala> val nestedNumbers = List(List(1, 2), List(3, 4))
 nestedNumbers: List[List[Int]] = List(List(1, 2), List(3, 4))

 scala> nestedNumbers.flatMap(x => x.map(_ * 2))
 res0: List[Int] = List(2, 4, 6, 8)
Scala – Collections


 Functional Combinators
 scala> val lista = List(p1, p2, p3)
 lista: List[Pessoa] = List(Pessoa(Joao,83), Pessoa(Maria,78),
 Pessoa(Isaias,33))

 scala> lista.filter(pessoa => pessoa.idade > 70)
 res2: List[Pessoa] = List(Pessoa(Joao,83), Pessoa(Maria,78))
Scala – Collections




Scalaz Combinator




 scala> List(10, 20, 30) |@| List(1, 2, 3) apply (_ * _)


 res0: List[Int] = List(10, 20, 30, 20, 40, 60, 30, 60, 90)
Scala – Collections


 Conversões Java / Scala
 import scala.collection.JavaConversions._

     val sl = new scala.collection.mutable.ListBuffer[Int]
     val jl : java.util.List[Int] = sl
     val sl2 : scala.collection.mutable.Buffer[Int] = jl
     assert(sl eq sl2)

 Conversões Mão Dupla
 scala.collection.Iterable <=> java.lang.Iterable
 scala.collection.Iterable <=> java.util.Collection
 scala.collection.Iterator <=> java.util.{ Iterator, Enumeration }
 scala.collection.mutable.Buffer <=> java.util.List
 scala.collection.mutable.Set <=> java.util.Set
 scala.collection.mutable.Map <=> java.util.{ Map, Dictionary }
 scala.collection.mutable.ConcurrentMap <=>
 java.util.concurrent.ConcurrentMap
Scala – Collections


 Conversões Java / Scala
 Conversões Mão Única
 scala.collection.Iterable <=> java.lang.Iterable
 scala.collection.Iterable <=> java.util.Collection
 scala.collection.Iterator <=> java.util.{ Iterator, Enumeration }
 scala.collection.mutable.Buffer <=> java.util.List
 scala.collection.mutable.Set <=> java.util.Set
 scala.collection.mutable.Map <=> java.util.{ Map, Dictionary }
 scala.collection.mutable.ConcurrentMap <=>
 java.util.concurrent.ConcurrentMap
Scala – Actors


 Scala Actors

  Abstração em processos Assíncronos

  Comunicação através de envio e recepção de
 mensagens

  Tratamento de uma mensagem por vez

  Melhor utilizado em cenários que podem ser divididos
 em várias etapas
Scala – Actors


 Scala Actors
 class Ping(count: Int, pong: Actor) extends Actor {
   def act() {
     var pingsLeft = count - 1
     pong ! Ping
     loop {
       react {
         case Pong =>
            if (pingsLeft % 1000 == 0)
              println("Ping: pong")
            if (pingsLeft > 0) {
              pong ! Ping
              pingsLeft -= 1
            } else {
              println("Ping: stop")
              pong ! Stop
              exit()
            }
       }
     }
   }
 }
Scala – Actors


 Scala Actors
 class Pong extends Actor {
   def act() {
     var pongCount = 0
     loop {
       react {
         case Ping =>
            if (pongCount % 1000 == 0)
              println("Pong: ping "+pongCount)
            sender ! Pong
            pongCount += 1
         case Stop =>
            println("Pong: stop")
            exit()
       }
     }
   }
 }

 object pingpong extends Application {
   val pong = new Pong
   val ping = new Ping(100000, pong)
   ping.start
   pong.start
 }
Scala – Actors


 Akka Actors
Scala – Ferramentas de Teste


  ScalaTest (http://www.scalatest.org)
   Test Driven Development (TDD)
    Behaviour-Driven Development (BDD)
    Funcional, integração e aceitação
 import org.scalatest.FunSpec

 class ExampleSpec extends FunSpec {

   describe("A Stack") {

      it("should pop values in last-in-first-out order") (pending)

     it("should throw NoSuchElementException if an empty stack is
 popped") (pending)
   }
 }
Scala – Ferramentas de Teste


  specs2 (http://etorreborre.github.com/specs2/)
    Testes unitários
    Testes de aceitação

 import org.specs2.mutable._

   class HelloWorldSpec extends Specification {

       "The 'Hello world' string" should {
         "contain 11 characters" in {
           "Hello world" must have size(11)
         }
         "start with 'Hello'" in {
           "Hello world" must startWith("Hello")
         }
         "end with 'world'" in {
           "Hello world" must endWith("world")
         }
       }
   }
Scala – IDEs




      http://scala-ide.org/




      http://www.jetbrains.com/idea/
Scala – Ferramentas de Build




  SBT (https://github.com/harrah/xsbt/wiki)

  MAVEN (http://maven.apache.org/)
Scala – Frameworks Web (Full)
Scala – Frameworks Web Java Compatíveis com Scala




            Spring MVC                         V-Raptor

                                              http://www.contasrefeitas.com.br/
Scala – Twitter
Scala – Timeline Twitter

  Mais de 100 milhões de usuários ativos

  250 mil Tweets por dia
  3 mil tweets / segundo – Média diária
  5 mil tweets / segundo – Pico
  Mais de 10 mil Tweets / segundo (Super Bowl)

  Poll-based
  200 mil queries por segundo
  Latência 1 milesegundo na média

  Buscas
  30 mil queries por segundo


                           https://github.com/twitter/finagle
Scala
Scala
Perguntas?




             ?
Obrigado
      isaias.barroso@strategiatec.com.br
      @isaias_barroso

Más contenido relacionado

La actualidad más candente

Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scalaRuslan Shevchenko
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Yardena Meymann
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaOstap Andrusiv
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scalascalaconfjp
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scaladjspiewak
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.Ruslan Shevchenko
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.Brian Hsu
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Andrea Zaza
 

La actualidad más candente (18)

scala
scalascala
scala
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with Scala
 
All about scala
All about scalaAll about scala
All about scala
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.
 
Programming Android Application in Scala.
Programming Android Application in Scala.Programming Android Application in Scala.
Programming Android Application in Scala.
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
 
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
 

Similar a Scala uma poderosa linguagem para a jvm

Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaMichael Stal
 
Intro to scala
Intro to scalaIntro to scala
Intro to scalaJoe Zulli
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basicswpgreenway
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Scala - just good for Java shops?
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?Sarah Mount
 
Scala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosGenevaJUG
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanJimin Hsieh
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghStuart Roebuck
 

Similar a Scala uma poderosa linguagem para a jvm (20)

Scala presentationjune112011
Scala presentationjune112011Scala presentationjune112011
Scala presentationjune112011
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Scala - just good for Java shops?
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?
 
Scala
ScalaScala
Scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala - core features
Scala - core featuresScala - core features
Scala - core features
 
Scala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian DragosScala at GenevaJUG by Iulian Dragos
Scala at GenevaJUG by Iulian Dragos
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 

Último

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Último (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Scala uma poderosa linguagem para a jvm

  • 1. Scala Uma poderosa linguagem para a JVM Isaías Cristiano Barroso Strategia Tecnologia 29 de Maio de 2012
  • 2.  Criador da linguagem Scala  Compilador de Referência JAVA  Co-autor Java Generics Martin Odersky EPFL - École polytechnique fédérale de Lausanne
  • 3. Scala – O que disse James Gosling? "If I were to pick a language to use today other than Java, it would be Scala." -- James Gosling, criador do Java
  • 4. Scala – Visão Geral  Compatibilidade com a JVM (JDK 1.5+)  Linguagem funcional  Empresa Typesafe (www.typesafe.com) mantem junto a comunidade a linguagem Scala.
  • 5. Scala – Comparação de Byte Code public class Hello { class Hello { public String sayHello(String name){ def sayHello(name: String):String = { return "Hello " + name; "Hello " + name } } } } Código JAVA vs Scala
  • 6. Scala – Comparação de Byte Code public class Hello extends public class Hello extends Object java.lang.Object{ implements scala.ScalaObject{ public Hello(); public java.lang.String Code: sayHello(java.lang.String); 0: aload_0 Code: 1: invokespecial #1; //Method 0: new #7; //class java/lang/Object."<init>":()V scala/collection/mutable/StringBuilder 4: return 3: dup 4: invokespecial #11; //Method public java.lang.String scala/collection/mutable/StringBuilder."<i sayHello(java.lang.String); nit>":()V Code: 7: ldc #14; //String Hello 0: new #2; //class 9: invokevirtual #18; //Method java/lang/StringBuilder scala/collection/mutable/StringBuilder.app 3: dup end:(Ljava/lang/Object;)Lscala/collection/ 4: invokespecial #3; //Method mutable/StringBuilder; java/lang/StringBuilder."<init>":()V 12: aload_1 7: ldc #4; //String Hello 13: invokevirtual #18; //Method 9: invokevirtual #5; //Method scala/collection/mutable/StringBuilder.app java/lang/StringBuilder.append:(Ljava/lang/ end:(Ljava/lang/Object;)Lscala/collection/ String;)Ljava/lang/StringBuilder; mutable/StringBuilder; 12: aload_1 16: invokevirtual #22; //Method 13: invokevirtual #5; //Method scala/collection/mutable/StringBuilder.toS java/lang/StringBuilder.append:(Ljava/lang/ tring:()Ljava/lang/String; String;)Ljava/lang/StringBuilder; 19: areturn 16: invokevirtual #6; //Method public Hello(); java/lang/StringBuilder.toString: Code: ()Ljava/lang/String; 0: aload_0 19: areturn 1: invokespecial #30; //Method java/lang/Object."<init>":()V } 4: return }
  • 7. Scala – Orientada a Objetos Cada valor é um objeto
  • 8. Scala – Orientada a Objetos Cada valor é um objeto scala> val a = 10 a: Int = 10 scala> a. % & * + - / > >= >> >>> ^ asInstanceOf isInstanceOf toByte toChar toDouble toFloat toInt toLong toShort toString unary_+ unary_- unary_~ | scala> a.+(10) res0: Int = 20 scala> a + 10 res1: Int = 20
  • 9. Scala – Orientada a Objetos Classes class Point(xc: Int, yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy } override def toString(): String = "(" + x + ", " + y + ")"; } scala> val p = new Point(10, 20) p: Point = (10, 20) scala> p res0: Point = (10, 20)
  • 10. Scala – Orientada a Objetos Classes (Scala vs JAVA) class Point(xc: Int, yc: Int) { var x: Int = xc var y: Int = yc def move(dx: Int, dy: Int) { x = x + dx y = y + dy } override def toString(): String = "(" + x + ", " + y + ")"; } public class Point { int x; int y; Point(int xc, int yc){ x = xc; y = yc; } public void move(int dx, int dy){ x = x + dx; y = y + dy; } public String toString() { return "(" + x + ", " + y + " )"; } }
  • 11. Scala – Orientada a Objetos Traits  Utilizadas para definir tipos e métodos suportados de um objeto  Similar as interfaces JAVA  Podem ser parcialmente implementadas  Não tem parâmetros para construtores
  • 12. Scala – Orientada a Objetos Traits trait Similarity { def isSimilar(x: Any): Boolean def isNotSimilar(x: Any): Boolean = !isSimilar(x) } class Point(xc: Int, yc: Int) extends Similarity { var x: Int = xc var y: Int = yc def isSimilar(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x } object TraitsTest extends Application { val p1 = new Point(2, 3) val p2 = new Point(2, 4) val p3 = new Point(3, 3) println(p1.isNotSimilar(p2)) // false println(p1.isNotSimilar(p3)) // true println(p1.isNotSimilar(2)) // true }
  • 13. Scala – Orientada a Objetos “object”  Equivalente a uma Singleton class to Java … object TraitsTest extends Application { val p1 = new Point(2, 3) val p2 = new Point(2, 4) val p3 = new Point(3, 3) println(p1.isNotSimilar(p2)) // false println(p1.isNotSimilar(p3)) // true println(p1.isNotSimilar(2)) // true }
  • 14. Scala – Orientada a Objetos “Companion Object”  Mesmo nome da classe  Definido no mesmo arquivo da classe  Possui acesso a todos os membros da classe  Muito utilizado como Factory http://www.scala-lang.org/api
  • 15. Scala – Orientada a Objetos “Companion Object” http://www.scala-lang.org/api
  • 16. Scala – Orientada a Objetos “object” scala> val bd = BigDecimal("123.43") bd: scala.math.BigDecimal = 123.43 scala> val bd = BigDecimal(123.43) bd: scala.math.BigDecimal = 123.43 “apply Method” scala> val bd = BigDecimal.apply("123.43") bd: scala.math.BigDecimal = 123.43 scala> val bd = BigDecimal.apply(123.43) bd: scala.math.BigDecimal = 123.43
  • 17. “Case Class” Scala – Orientada a Objetos  Classes Scala como qualquer outra  Exportam seus parâmetros de construtores  Método apply criado automaticamente  Permite decomposição funcional através de “pattern matching”
  • 18. Scala – Orientada a Objetos “Case Class” scala> abstract class Pessoa defined class Pessoa scala> case class PessoaFisica(nome: String, idade: Int) extends Pessoa defined class PessoaFisica scala> case class PessoaJuricia(nome: String, tempoAbertura: Int) extends Pessoa defined class PessoaJuricia toString implementado automaticamente scala> val pf = PessoaFisica("Joao", 83) pf: PessoaFisica = PessoaFisica(Joao,83) scala> val pj = PessoaJuridica("The Corp", 4) pj: PessoaJuridica = PessoaJuridica(The Corp,4) scala> println(pf) PessoaFisica(Joao,83) scala> println(pj) PessoaJuridica(The Corp,4)
  • 19. Scala – Orientada a Objetos “Case Class” equals implementado automaticamente scala> val x = PessoaFisica("Joao", 83) x: PessoaFisica = PessoaFisica(Joao,83) scala> val y = PessoaFisica("Joao", 83) y: PessoaFisica = PessoaFisica(Joao,83) scala> val z = PessoaFisica("Maria", 78) z: PessoaFisica = PessoaFisica(Maria,78) scala> x == y res3: Boolean = true scala> x == z res4: Boolean = false apply de case class em ação scala> val pf = PessoaFisica("Isaias") <console>:9: error: not enough arguments for method apply: (nome: String, idade: Int)PessoaFisica in object PessoaFisica. Unspecified value parameter idade. val pf = PessoaFisica("Isaias")
  • 20. Scala – Orientada a Objetos “Pattern Matching” def testPM(p: Pessoa){ p match { case PessoaFisica(nome, idade) => println("Nome: " + nome) println("Idade: " + idade) case PessoaJuridica(nome, tempo) => println("Nome: " + nome) println("Tempo: " + tempo) case _ => println("Tipo Nao Esperado") } } “Sealed Case Class” scala> sealed abstract class Pessoa defined class Pessoa
  • 21. Scala – Linguagem Funcional  High Order Functions (Funções podem receber e retornar funções)  Programação concorrente  Encoraja a utilização de variáveis imutáveis (var x val)  Estaticamente tipada
  • 22. Scala – Linguagem Funcional Fatorial def fac(n : Int) = { var r = 1 for (i <- 1 to n) r = r * i r } scala> fac(5) res0: Int = 120 Fatorial - Recursivo def fac(n: Int): Int = if (n <= 0) 1 else Não n * fac(n – 1) Otimizado scala> fac(5) res0: Int = 120
  • 23. Scala – Linguagem Funcional @tailrec def fac(n: Int): Int = if (n <= 0) 1 else n * fac(n – 1) <console>:10: error: could not optimize @tailrec annotated method fac: it contains a recursive call not in tail position if (n <= 0) 1 else n * fac(n -1) ^ Fatorial – Tail Recursive @tailrec def factorial(accumulator: Int, number: Int) : Int = { if(number == 1) return accumulator factorial(number * accumulator, number - 1) } Otimizado
  • 24. Scala – Linguagem Funcional Melhorando a função def factorial(number: Int) : Int = { def factorialWithAccumulator(accumulator: Int, number: Int) : Int = { if (number == 1) return accumulator else factorialWithAccumulator(accumulator * number, number - 1) } factorialWithAccumulator(1, number) }
  • 25. Scala – Linguagem Funcional Functions def <nome>(parametros) : <retorno> def funcao1() { println(“Funcao sem parametro e sem retorno”) } def funcao2(x: Int, y: String) { println(“Funcao com parametro e sem retorno”) } def funcao3(x: Int, y: String) = { println(“Funcao com parametros e retorno implicito”) “retorno” } def funcao4(x: Int, y: String):String = { println(“Funcao com parametros e retorno explicito”) “retorno” }
  • 26. Scala – Linguagem Funcional Funções Anônimas scala> (x: Int) => x + 1 res0: Int => Int = <function1> scala> res0(1) res1: Int = 2 Funções como valores scala> val inc = (x: Int) => x + 1 inc: Int => Int = <function1> scala> inc(10) res0: Int = 11
  • 27. Scala – Linguagem Funcional High Order Functions scala> def soma(x:Int, y:Int):Int = { | x+y | } soma: (x: Int, y: Int)Int scala> soma(10,20) res5: Int = 30 scala> def withLog(f:(Int, Int) => Int, x:Int, y:Int):Int = { | println("Iniciando execucao da funcao") | val result = f(x,y) | println("Finalizando Execucao") | result | } withLog: (f: (Int, Int) => Int, x: Int, y: Int)Int scala> withLog(soma, 10, 20) Iniciando execucao da funcao Finalizando Execucao res6: Int = 30
  • 28. Scala – Linguagem Funcional Querulous queryEvaluator.transaction { transaction => transaction.select("SELECT ... FOR UPDATE", ...) transaction.execute("INSERT INTO users VALUES (?, ?)", 1, "Jacques") transaction.execute("INSERT INTO users VALUES (?, ?)", 2, "Luc") } // def transaction[T](f: Transaction => T) = { withTransaction { transaction => transaction.begin() try { val rv = f(transaction) transaction.commit() rv } catch { case e: Throwable => transaction.rollback() throw e } } } private def withTransaction[A](f: Transaction => A) = { database.withConnection { connection => f(new Transaction(queryFactory, connection)) } }
  • 29. Scala – Linguagem Funcional Implicit Parameters scala> def addOne(implicit x:Int) = { | x+1 | } addOne: (implicit x: Int)Int scala> addOne <console>:9: error: could not find implicit value for parameter x: Int addOne ^ scala> implicit val x = 10 x: Int = 10 scala> addOne res1: Int = 11 scala> addOne(35) res2: Int = 36
  • 30. Scala – Linguagem Funcional Implicit Conversions scala> class RichString(s:String) defined class RichString scala> implicit def string2RichString(s:String) = new RichString(s) string2RichString: (s: String)RichString scala> val x = "Isaias" x: java.lang.String = Isaias scala> def printString(r: RichString) { | println(r) | } printString: (r: RichString)Unit scala> printString(x) $line1.$read$$iw$$iw$RichString@6fafc4c2 scala>
  • 31. Scala – Linguagem Funcional Mixin Class Composition abstract class AbsIterator { type T def hasNext: Boolean def next: T } trait RichIterator extends AbsIterator { def foreach(f: T => Unit) { while (hasNext) f(next) } } class StringIterator(s: String) extends AbsIterator { type T = Char private var i = 0 def hasNext = i < s.length() def next = { val ch = s charAt i; i += 1; ch } } object StringIteratorTest { def main(args: Array[String]) { class Iter extends StringIterator(args(0)) with RichIterator val iter = new Iter iter foreach println } }
  • 32. Scala – Collections Lists val numbers = List(1, 2, 3, 4) numbers: List[Int] = List(1, 2, 3, 4) Sets scala> Set(1, 1, 2) res0: scala.collection.immutable.Set[Int] = Set(1, 2)
  • 33. Scala – Collections Tuplas scala> val hostPort = ("localhost", 80) hostPort: (String, Int) = (localhost, 80) scala> hostPort._1 res5: java.lang.String = localhost scala> hostPort._2 res6: Int = 80 Tuplas – Pattern Matching hostPort match { case ("localhost", port) => ... case (host, port) => ... }
  • 34. Scala – Collections Map Map(1 -> 2) res0: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) Map("foo" -> "bar") res0: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(foo -> bar) Map(1 -> Map("foo" -> "bar")) res0: scala.collection.immutable.Map[Int,scala.collection.immutable.Map[jav a.lang.String,java.lang.String]] = Map(1 -> Map(foo -> bar))
  • 35. Scala – Collections Option val map = Map("Hi" -> "Dan", "Hello" -> "Jane") val result = map.get( "Hello" ) result match { case None => println("No value found for key!") case Some(x) => print("Found value" + x) } scala> map.get("NoKey") res5: Option[java.lang.String] = None
  • 36. Scala – Collections Functional Combinators “A saída de uma função será utilizada como entrada para outra função” scala> val numbers = List(1, 2, 3, 4) numbers: List[Int] = List(1, 2, 3, 4) scala> numbers.map((i:Int) => i * 2) res0: List[Int] = List(2, 4, 6, 8) scala> numbers.map(_ * 2) res1: List[Int] = List(2, 4, 6, 8) scala> numbers.foreach((i: Int) => i * 2) // Sem retorno scala> var value = 0 value: Int = 0 scala> numbers.foreach((i: Int) => value = i*2) scala> value res1: Int = 28
  • 37. Scala – Collections Functional Combinators scala> numbers.filter((i: Int) => i%2==0) res0: List[Int] = List(2, 4) scala> def isEven(i: Int): Boolean = i % 2 == 0 isEven: (i: Int)Boolean scala> numbers.filter(isEven _) res2: List[Int] = List(2, 4) scala> List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).partition((i: Int) => i > 5) res0: (List[Int], List[Int]) = (List(6, 7, 8, 9, 10),List(1, 2, 3, 4, 5)) scala> res0._1 res1: List[Int] = List(6, 7, 8, 9, 10) scala> res0._2 res2: List[Int] = List(1, 2, 3, 4, 5)
  • 38. Scala – Collections Functional Combinators scala> val numbers = List(1,2,3,4,5,6,7,8,9,10) numbers: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> numbers.find((i: Int) => i > 5) res5: Option[Int] = Some(6) scala> numbers.drop(5) res0: List[Int] = List(6, 7, 8, 9, 10) scala> numbers.dropWhile((i: Int) => i < 6) res0: List[Int] = List(6, 7, 8, 9, 10)
  • 39. Scala – Collections Functional Combinators scala> numbers.foldLeft(0)((m: Int, n: Int) => m + n) res0: Int = 55 scala> numbers.foldLeft(0) { (m: Int, n: Int) => println("m: " + m + " n: " + n); m + n } m: 0 n: 1 m: 1 n: 2 m: 3 n: 3 m: 6 n: 4 m: 10 n: 5 m: 15 n: 6 m: 21 n: 7 m: 28 n: 8 m: 36 n: 9 m: 45 n: 10 res0: Int = 55
  • 40. Scala – Collections Functional Combinators scala> numbers.foldRight(0)((m: Int, n: Int) => m + n) res0: Int = 55 scala> numbers.foldRight(0)((m: Int, n: Int) => println("m: " + m + " n: " + n); m + n) m: 10 n: 0 m: 9 n: 10 m: 8 n: 19 m: 7 n: 27 m: 6 n: 34 m: 5 n: 40 m: 4 n: 45 m: 3 n: 49 m: 2 n: 52 m: 1 n: 54 res0: Int = 55
  • 41. Scala – Collections Functional Combinators scala> List(List(1, 2), List(3, 4)).flatten res0: List[Int] = List(1, 2, 3, 4) scala> val nestedNumbers = List(List(1, 2), List(3, 4)) nestedNumbers: List[List[Int]] = List(List(1, 2), List(3, 4)) scala> nestedNumbers.flatMap(x => x.map(_ * 2)) res0: List[Int] = List(2, 4, 6, 8)
  • 42. Scala – Collections Functional Combinators scala> val lista = List(p1, p2, p3) lista: List[Pessoa] = List(Pessoa(Joao,83), Pessoa(Maria,78), Pessoa(Isaias,33)) scala> lista.filter(pessoa => pessoa.idade > 70) res2: List[Pessoa] = List(Pessoa(Joao,83), Pessoa(Maria,78))
  • 43. Scala – Collections Scalaz Combinator scala> List(10, 20, 30) |@| List(1, 2, 3) apply (_ * _) res0: List[Int] = List(10, 20, 30, 20, 40, 60, 30, 60, 90)
  • 44. Scala – Collections Conversões Java / Scala import scala.collection.JavaConversions._ val sl = new scala.collection.mutable.ListBuffer[Int] val jl : java.util.List[Int] = sl val sl2 : scala.collection.mutable.Buffer[Int] = jl assert(sl eq sl2) Conversões Mão Dupla scala.collection.Iterable <=> java.lang.Iterable scala.collection.Iterable <=> java.util.Collection scala.collection.Iterator <=> java.util.{ Iterator, Enumeration } scala.collection.mutable.Buffer <=> java.util.List scala.collection.mutable.Set <=> java.util.Set scala.collection.mutable.Map <=> java.util.{ Map, Dictionary } scala.collection.mutable.ConcurrentMap <=> java.util.concurrent.ConcurrentMap
  • 45. Scala – Collections Conversões Java / Scala Conversões Mão Única scala.collection.Iterable <=> java.lang.Iterable scala.collection.Iterable <=> java.util.Collection scala.collection.Iterator <=> java.util.{ Iterator, Enumeration } scala.collection.mutable.Buffer <=> java.util.List scala.collection.mutable.Set <=> java.util.Set scala.collection.mutable.Map <=> java.util.{ Map, Dictionary } scala.collection.mutable.ConcurrentMap <=> java.util.concurrent.ConcurrentMap
  • 46. Scala – Actors Scala Actors  Abstração em processos Assíncronos  Comunicação através de envio e recepção de mensagens  Tratamento de uma mensagem por vez  Melhor utilizado em cenários que podem ser divididos em várias etapas
  • 47. Scala – Actors Scala Actors class Ping(count: Int, pong: Actor) extends Actor { def act() { var pingsLeft = count - 1 pong ! Ping loop { react { case Pong => if (pingsLeft % 1000 == 0) println("Ping: pong") if (pingsLeft > 0) { pong ! Ping pingsLeft -= 1 } else { println("Ping: stop") pong ! Stop exit() } } } } }
  • 48. Scala – Actors Scala Actors class Pong extends Actor { def act() { var pongCount = 0 loop { react { case Ping => if (pongCount % 1000 == 0) println("Pong: ping "+pongCount) sender ! Pong pongCount += 1 case Stop => println("Pong: stop") exit() } } } } object pingpong extends Application { val pong = new Pong val ping = new Ping(100000, pong) ping.start pong.start }
  • 49. Scala – Actors Akka Actors
  • 50. Scala – Ferramentas de Teste  ScalaTest (http://www.scalatest.org) Test Driven Development (TDD)  Behaviour-Driven Development (BDD)  Funcional, integração e aceitação import org.scalatest.FunSpec class ExampleSpec extends FunSpec { describe("A Stack") { it("should pop values in last-in-first-out order") (pending) it("should throw NoSuchElementException if an empty stack is popped") (pending) } }
  • 51. Scala – Ferramentas de Teste  specs2 (http://etorreborre.github.com/specs2/)  Testes unitários  Testes de aceitação import org.specs2.mutable._ class HelloWorldSpec extends Specification { "The 'Hello world' string" should { "contain 11 characters" in { "Hello world" must have size(11) } "start with 'Hello'" in { "Hello world" must startWith("Hello") } "end with 'world'" in { "Hello world" must endWith("world") } } }
  • 52. Scala – IDEs http://scala-ide.org/ http://www.jetbrains.com/idea/
  • 53. Scala – Ferramentas de Build  SBT (https://github.com/harrah/xsbt/wiki)  MAVEN (http://maven.apache.org/)
  • 54. Scala – Frameworks Web (Full)
  • 55. Scala – Frameworks Web Java Compatíveis com Scala Spring MVC V-Raptor http://www.contasrefeitas.com.br/
  • 57. Scala – Timeline Twitter  Mais de 100 milhões de usuários ativos  250 mil Tweets por dia 3 mil tweets / segundo – Média diária 5 mil tweets / segundo – Pico Mais de 10 mil Tweets / segundo (Super Bowl)  Poll-based 200 mil queries por segundo Latência 1 milesegundo na média  Buscas 30 mil queries por segundo https://github.com/twitter/finagle
  • 58. Scala
  • 59. Scala
  • 61. Obrigado isaias.barroso@strategiatec.com.br @isaias_barroso