Paradigmas de programação funcional + objetos no liquidificador com scala

1,383 views

Published on

Aspectos introdutórios de Scala e suas diferenças em relação a Java, e como integrar as duas linguagens na mesma aplicação

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,383
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Paradigmas de programação funcional + objetos no liquidificador com scala

  1. 1. Paradigmas de programação Funcional + Objetos no liquidificador com Scala @abstractj Sunday, September 12, 2010
  2. 2. Scala Funcional OO • Imutabilidade • Escopo • Suporte a concorrência • Regras de visibilidade • Inferência de tipos • Código mais conciso • Polimorfismo • ...e outras cositas mas Sunday, September 12, 2010
  3. 3. Funções 1 class MandaProCaps { 2 def upper(strings: String*): Seq[String] = { 3 strings.map((s:String) => s.toUpperCase()) 4 } 5 } 6 val manda = new MandaProCaps 7 Console.println(manda.upper ("AfddAfdT","dfaAfddA","dfdaAfdAfd")) Sunday, September 12, 2010
  4. 4. Simplicidade 1 public class User { 1 class User { 2 2 3 public static final int idade = 10 3 val idade:Int = 20 4 4 var login = "" 5 private String login; 5 var password = "" 6 private String password; 6 var name = "" 7 private String name; 7 } 8 27 //gets e sets para cada atributo 28 } 1 val cores = List("Azul", "Vermelho", "Abobora", "Azul Piscina", "Azul Celeste") 2 for (cor <- cores 3 if cor.contains("Azul") 4 ) println(cor) Sunday, September 12, 2010
  5. 5. Singletons 12 object RunInferenceTypes{ 13 def main(args: Array[String]) = { 15 val infer = new InferenceTypes 16 println("Calcula: " + infer.calcula(4)) 17 println("Faz nada: " + infer.naoFazNada) 18 println("X: " + infer.x) 19 println("Y: " + infer.y) 20 } 21 } Sunday, September 12, 2010
  6. 6. Inferência de tipos 1 class InferenceTypes { 2 val x = 1 + 3 * 6 3 val y = x.toString 4 5 def calcula(valor: Int) = { 6 "Valor: " + x + y + valor 7 } 8 def naoFazNada { 9 println("Faco nada nao") 10 } 11 } Sunday, September 12, 2010
  7. 7. Tuples 1 class MilkShake { 2 def opcoes(pedido: Any) = { 3 ("banana", "maca", "morango") 4 } 5 } 6 object AskMilkShake { 7 def main(args: Array[String]) = { 8 val shake = new MilkShake 9 val (x, y, z) = shake.opcoes("Tem o que?") 10 11 println(shake.opcoes("Tem o que?")) 12 println("Shake: " + x) 13 println("Shake: " + y) 14 println("Shake: " + z) 15 } 16 } Sunday, September 12, 2010
  8. 8. Pattern Matching 1 val tipos = List(1, "QconSP", 10.2) 2 for (tipo <- tipos) { 3 tipo match { 4 case a: Int => println("Inteiro: " + a) 5 case b: String => println("String: " + b) 6 case c: Double => println("Double: " + c) 7 } 8 } 1 val lista1 = List(1, 3, 23, 90) 2 val lista2 = List(4, 18, 52, 7) 3 4 for (l <- List(lista1, lista2)) { 5 l match { 6 case List(_, 3, _, _) => println("3") 7 case List(_, _, _, 7) => println("7") 8 } 9 } Sunday, September 12, 2010
  9. 9. Deep Match 1 //Script Scala 2 case class Musica(nome: String, estilo: String) 3 4 val rock = new Musica("Metallica", "Rock") 5 val classica = new Musica("Bach", "Classico") 6 7 for (musica <- List(rock, classica)) { 8 musica match { 9 case Musica("Metallica", "Rock") => println("Metaleiro") 10 case Musica("Bach", "Classico") => println("Erudito") 11 } 12 } Sunday, September 12, 2010
  10. 10. Yielding 78 def list = { 79 80 val usersDatabase: List[User] = this.dao.listAll() 81 82 val users = for { 83 user <- usersFromDatabase 84 user.setLogin(s.getLogin()) 85 user.setName(s.getName()) 86 } yield user 87 result.include("users", users) 88 } Sunday, September 12, 2010
  11. 11. E minha app Java?!!! Sunday, September 12, 2010
  12. 12. Scala & VRaptor 62 @Path(Array("/")) 63 @Get 64 def home() = { 65 dao.refresh(userInfo.getUser()) 66 result.include("dvdTypes", DvdType.values) 67 } 69 @Path("/") 70 @Get 71 public void home() { 72 dao.refresh(userInfo.getUser()); 73 result.include("dvdTypes", DvdType.values()); 74 } Sunday, September 12, 2010
  13. 13. Scala & VRaptor 62 @Path(Array("/")) 63 @Get 64 def home() = { 65 dao.refresh(userInfo.getUser()) 66 result.include("dvdTypes", DvdType.values) 67 } 69 @Path("/") 70 @Get 71 public void home() { 72 dao.refresh(userInfo.getUser()); 73 result.include("dvdTypes", DvdType.values()); 74 } Sunday, September 12, 2010
  14. 14. Ferramentas Desenvolvimento Build • Scala IDE • SBT • Vim • Gradle • Netbeans • Intellij Sunday, September 12, 2010
  15. 15. Pra onde ir?! - http://www.scala-lang.org - http://www.planetscala.com - http://days2010.scala-lang.org - http://github.com/abstractj Sunday, September 12, 2010

×