Jugar Introduccion a Scala

4,228 views

Published on

Introducción a Scala:
* JVM / Java Interop
* Type inference
* Muy Conciso
* REPL
* Collections
* Promueve buenas prácticas
* Functional + OO
* Pattern Matching
* Case Class
* Implicit conversions
* Traits
* Some/None/Option

Published in: Technology, News & Politics
  • Be the first to comment

Jugar Introduccion a Scala

  1. 1. Escala el Everest con Scala<br />http://slidesha.re/nRVC7x<br />
  2. 2. Soy …<br />Gustavo Arjones<br />gustavo@socialmetrix.com<br />@arjones<br />CTO – Socialmetrix<br />Vivo en BA desde 2007<br />Interés:<br />Sistemasdistribuídos<br />SOA<br />Databases<br />Text Mining / NLP<br />Big Data<br />Social Media<br />Data Visualization<br />
  3. 3. “If I were to pick a language to use today other than Java, it would be Scala”<br />James Gosling<br />
  4. 4. Madurez …<br />Professional Services / Training<br />
  5. 5. Porque me gusta?<br />JVM / Java Interop<br />Type inference<br />MuyConciso<br />REPL<br />Lib de Collections<br />Promuevebuenasprácticas<br />Functional + OO<br />Pattern Matching<br />Case Class<br />Implicit conversions<br />Traits<br />Some/None/Option<br />
  6. 6. JVM / Java Interop<br />
  7. 7. Type Inference<br />valcountryByLanguage : Map[String, List[String]] = Map(<br /> "es"->List("ar","es", "mx"), <br /> "pt" -> List("br","pt"), <br /> "en" -> List("uk","us")<br />)<br />valcountryByLanguage = Map(<br /> "es"->List("ar","es", "mx"), <br /> "pt" -> List("br","pt"), <br /> "en" -> List("uk","us")<br />)<br />countryByLanguage: scala.collection.immutable.Map[String,List[String]]<br />
  8. 8. MuyConciso<br />def +(v:Int) = v+1<br />objA==objB<br />No esnecesario:<br />()<br />;<br />return<br />.<br />
  9. 9. REPL (Equiv. Ruby IRB)<br />
  10. 10. Collections<br />
  11. 11. Collections<br />
  12. 12. Buzz de los lenguajes<br />
  13. 13. Procesando Delicious<br />
  14. 14. https://gist.github.com/1123656<br />
  15. 15. Promuevebuenaspracticas … sin limitarte!<br />Remueve Boilerplate <br />Inmutabilidad<br />Uso de funciones<br />No side-effect<br />Muchos helpers yconstructores<br />Share-nothing<br />Listopara Multi-core<br />
  16. 16. Functional programming<br />cat FILE | grep"@jugar.com.ar" | wc<br />valplaces = List("Buenos Aires", "Bogota", "DF", "Sao Paulo", "New York")<br />places.filter((p:String) => p.startsWith("B"))<br />places.filter(p => p.startsWith("B"))<br />places.filter(_.startsWith("B"))<br />valmyFunc = (p:String) => p.startsWith("B")<br />places.filter(myFilter)<br />def myFilter(p:String) = p.startsWith("B")<br />places.filter(myFilter)<br />
  17. 17. Pattern Matching<br />case classPerson(valname: String, valage: Int)<br />valppl = List(Person("John", 25), Person("Paul", 27), Person("George", 22), Person("Ringo", 22))<br />ppl.foreach {<br /> _ match {<br />case Person("John", age) => println("John is " + age)<br />case Person(name, 22) => println(name + " is 22")<br />case _ => println("Don't know")<br /> }<br /> }<br />
  18. 18. Implicit conversions<br />Conversión de tipos<br />Adicionarcomportamiento en clasescerradas<br />Extender el lenguaje<br />
  19. 19. Implicit conversions<br />class RichInt(i: Int) {<br />def times(f: => Unit) = {<br />for (x <- 0 to i) f<br /> }<br /> }<br />new RichInt (5).times { println("hi!") }<br />implicit defintToRichInt(i: Int) = new RichInt(i)<br /> 5.times { println("nice!") }<br />
  20. 20. traits – Interfaces con anabólicos<br />trait NoNullAccessMap[K, V] extends java.util.Map[K, V] {<br />abstract override def get(key: Object): V = {<br />valvalue = super.get(key)<br />assert(value != null, "No value found " + key)<br />return value<br /> }<br /> }<br />trait NoOverwriteMap[K, V] extends java.util.Map[K, V] {<br />abstract override def put(key: K, value: V): V = {<br />assert(!containsKey(key), "Could not set " + key <br /> + " to " + value + ": it is already set to " +<br />get(key))<br />return super.put(key, value)<br /> }<br /> }<br />
  21. 21. traits – Interfaces con anabólicos<br />class StrictMap[K, V] extends HashMap[K, V]<br />with NoNullAccessMap[K, V]<br />with NoOverwriteMap[K, V]<br />valstateForCity = new HashMap[String, String]() <br />with NoNullAccessMap[String, String] <br />with NoOverwriteMap[String, String]<br />
  22. 22. Option<br />//previous approach: doesnt compile!<br />val name = getPerson("person123").name<br />//.get will throw a RuntimeException if it is null here<br />val name2 = getPerson("person123").get.name<br />//get the value, or use a default<br />val name3 = getPerson("person123").getOrElse("Name unknown")<br />//in some cases (not this one), pattern matching is nice<br />val name4 = getPerson("person123") match {<br /> case Some(name) => name<br /> case None => "Name Unknown”<br />}<br />
  23. 23. Take away<br />No es Java oScala, puedenconvivir!<br />AprenderScala me hacemejordesarrollador Java<br />Empezá con pequeñosproyectosparaprobar, ganáconfianza<br />La sintaxisparececompleja, después se despeja<br />
  24. 24. Pordóndeempezar<br />How we (mostly) moved from Java to Scala<br />http://bit.ly/rlw2Cx<br />Scala for Java Refugees<br />http://bit.ly/qmHTpm<br />Simply Scala<br />http://www.simplyscala.com<br />Typesafe Stack<br />http://bit.ly/qmcP0A<br />An introduction to Scala for Java Programmers<br />http://slidesha.re/oGvttM<br />Pragmatic Real-World Scala<br />http://slidesha.re/nfF0xK<br />Tools & Libraries<br />http://bit.ly/nple9Z<br />
  25. 25. Trabajamos con estastecnologías<br />Sumate a nuestroequipo!<br />gustavo@socialmetrix.com<br />
  26. 26. Gracias / Obrigado<br />;)<br />

×