jugar introduccion a scala

26
cala el Everest con Sca http://slidesha.re/nRVC7x

Upload: socialmetrix

Post on 18-May-2015

3.817 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Jugar Introduccion a Scala

Escala el Everest con Scala

http://slidesha.re/nRVC7x

Page 2: Jugar Introduccion a Scala

@arjones

Soy …

Gustavo Arjones

[email protected]

@arjones

CTO – Socialmetrix

Vivo en BA desde 2007

Interés:• Sistemas distribuídos• SOA• Databases• Text Mining / NLP• Big Data• Social Media• Data Visualization

Page 3: Jugar Introduccion a Scala

@arjones

“If I were to pick a language to use today

other than Java, it would be Scala”

James Gosling

Page 4: Jugar Introduccion a Scala

@arjones

Madurez …

Professional Services / Training

Page 5: Jugar Introduccion a Scala

@arjones

Porque me gusta?

• JVM / Java Interop• Type inference• Muy Conciso• REPL• Lib de Collections• Promueve buenas

prácticas

• Functional + OO• Pattern Matching• Case Class• Implicit conversions• Traits• Some/None/Option

Page 6: Jugar Introduccion a Scala

@arjones

JVM / Java Interop

Page 7: Jugar Introduccion a Scala

@arjones

Type Inference

val countryByLanguage : Map[String, List[String]] = Map("es"->List("ar","es", "mx"), "pt" -> List("br","pt"), "en" -> List("uk","us")

)

val countryByLanguage = Map("es"->List("ar","es", "mx"), "pt" -> List("br","pt"), "en" -> List("uk","us")

)

countryByLanguage: scala.collection.immutable.Map[String,List[String]]

Page 8: Jugar Introduccion a Scala

@arjones

Muy Conciso

No es necesario:();return.

def +(v:Int) = v+1

objA == objB

Page 9: Jugar Introduccion a Scala

@arjones

REPL (Equiv. Ruby IRB)

Page 10: Jugar Introduccion a Scala

@arjones

Collections

Page 11: Jugar Introduccion a Scala

@arjones

Collections

Page 12: Jugar Introduccion a Scala

@arjones

Buzz de los lenguajes

Page 13: Jugar Introduccion a Scala

@arjones

Procesando Delicious

Page 14: Jugar Introduccion a Scala

@arjones https://gist.github.com/1123656

Page 15: Jugar Introduccion a Scala

@arjones

Promueve buenas practicas … sin limitarte!

• Remueve Boilerplate • Inmutabilidad• Uso de funciones• No side-effect• Muchos helpers y constructores• Share-nothing• Listo para Multi-core

Page 16: Jugar Introduccion a Scala

@arjones

Functional programming

cat FILE | grep "@jugar.com.ar" | wc

val places = List("Buenos Aires", "Bogota", "DF", "Sao Paulo", "New York")

places.filter((p:String) => p.startsWith("B"))

places.filter(p => p.startsWith("B"))

places.filter(_.startsWith("B"))

val myFunc = (p:String) => p.startsWith("B")

places.filter(myFilter)

def myFilter(p:String) = p.startsWith("B")

places.filter(myFilter)

Page 17: Jugar Introduccion a Scala

@arjones

Pattern Matching

case class Person(val name: String, val age: Int)

val ppl = List(Person("John", 25), Person("Paul", 27), Person("George", 22), Person("Ringo", 22))

ppl.foreach { _ match { case Person("John", age) => println("John is " + age) case Person(name, 22) => println(name + " is 22") case _ => println("Don't know") } }

Page 18: Jugar Introduccion a Scala

@arjones

Implicit conversions

• Conversión de tipos

• Adicionar comportamiento en clases cerradas

• Extender el lenguaje

Page 19: Jugar Introduccion a Scala

@arjones

Implicit conversions

class RichInt(i: Int) { def times(f: => Unit) = { for (x <- 0 to i) f } }

new RichInt (5).times { println("hi!") }

implicit def intToRichInt(i: Int) = new RichInt(i)

5.times { println("nice!") }

Page 20: Jugar Introduccion a Scala

@arjones

traits – Interfaces con anabólicos

trait NoNullAccessMap[K, V] extends java.util.Map[K, V] { abstract override def get(key: Object): V = { val value = super.get(key) assert(value != null, "No value found " + key) return value } }

trait NoOverwriteMap[K, V] extends java.util.Map[K, V] { abstract override def put(key: K, value: V): V = { assert(!containsKey(key), "Could not set " + key

+ " to " + value + ": it is already set to " +

get(key)) return super.put(key, value) } }

Page 21: Jugar Introduccion a Scala

@arjones

traits – Interfaces con anabólicos

class StrictMap[K, V] extends HashMap[K, V] with NoNullAccessMap[K, V] with NoOverwriteMap[K, V]

val stateForCity = new HashMap[String, String]() with NoNullAccessMap[String, String] with NoOverwriteMap[String, String]

Page 22: Jugar Introduccion a Scala

@arjones

Option

//previous approach: doesnt compile!val name = getPerson("person123").name

//.get will throw a RuntimeException if it is null hereval name2 = getPerson("person123").get.name

//get the value, or use a defaultval name3 = getPerson("person123").getOrElse("Name unknown")

//in some cases (not this one), pattern matching is niceval name4 = getPerson("person123") match { case Some(name) => name case None => "Name Unknown”}

Page 23: Jugar Introduccion a Scala

@arjones

Take away

• No es Java o Scala, pueden convivir!

• Aprender Scala me hace mejor desarrollador Java

• Empezá con pequeños proyectos para probar, ganá confianza

• La sintaxis parece compleja, después se despeja

Page 24: Jugar Introduccion a Scala

@arjones

Por dónde empezar

How we (mostly) moved from Java to Scala

http://bit.ly/rlw2Cx

Scala for Java Refugees

http://bit.ly/qmHTpm

Simply Scala

http://www.simplyscala.com

Typesafe Stack

http://bit.ly/qmcP0A

An introduction to Scala for Java Programmers

http://slidesha.re/oGvttM

Pragmatic Real-World Scala

http://slidesha.re/nfF0xK

Tools & Libraries

http://bit.ly/nple9Z

Page 25: Jugar Introduccion a Scala

@arjones

Trabajamos con estas tecnologías

• Sumate a nuestro equipo!

[email protected]

Page 26: Jugar Introduccion a Scala

@arjones

Gracias / Obrigado

;)