• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Scala   uma poderosa linguagem para a jvm
 

Scala uma poderosa linguagem para a jvm

on

  • 1,537 views

Palestra VI Workshop de TI - COTEMIG

Palestra VI Workshop de TI - COTEMIG

Statistics

Views

Total Views
1,537
Views on SlideShare
1,537
Embed Views
0

Actions

Likes
3
Downloads
9
Comments
1

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • super guys..
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Scala   uma poderosa linguagem para a jvm Scala uma poderosa linguagem para a jvm Presentation Transcript

    • ScalaUma 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 – CollectionsScalaz 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