Pf con scala

Preview:

DESCRIPTION

 

Citation preview

PFRebecca Sliter, ThoughtWorks @rebeccasliter

con

Scala

utiliza el JVM

static-typed

utiliza el JVM

static-typed

orientado a objetos

utiliza el JVM

static-typed

orientado a objetos?

utiliza el JVM

static-typed

orientado a objetos

utiliza el JVM

static-typed

funcional

orientado a objetos

utiliza el JVM

static-typed

funcional

orientado a objetos

objeto-funcional

utiliza el JVM

funcionalQué hacea este lenguaje?

Funciones de orden superior

Funciones que tienen otra función como parametro

Funciones de orden superior

Funciones que tienen otra función como parametro

Funciones que retornan una función

Funciones de orden superior

object Almuerzo {def main(args: Array[String]) {

println( apply( postre, 50) )}

def apply(f: Int => String, v: Int) = f(v)

def postre(x: Int) = “Comi “ + x.toString() + “ frutas.”}

Funciones de orden superior

Comi 50 frutas.

object Almuerzo {def main(args: Array[String]) {

println( apply( postre, 50) )}

def apply(f: Int => String, v: Int) = f(v)

def postre(x: Int) = “Comi “ + x.toString() + “ frutas.”}

Evaluación estricta

scala> val a = b + 1; val b = 2;a: Int = 1b: Int = 2

Evaluación estrictaperezosa

scala> lazy val a = b + 1; lazy val b = 2;a: Int = <lazy>b: Int = <lazy>

scala> ares1: Int = 3

scala> bres1: Int = 2

Currying

scala> def add(a: Int)(b: Int) = a + badd: (a: Int)(b: Int)Int

Currying

scala> def add(a: Int)(b: Int) = a + badd: (a: Int)(b: Int)Int

scala> add(5)(6)res1: Int = 11

Currying

scala> def add(a: Int)(b: Int) = a + badd: (a: Int)(b: Int)Int

scala> add(5)(6)res1: Int = 11

scala> val addTen = add(10)_addTen: Int => Int = <function>

Currying

scala> def add(a: Int)(b: Int) = a + badd: (a: Int)(b: Int)Int

scala> add(5)(6)res1: Int = 11

scala> val addTen = add(10)_addTen: Int => Int = <function>

scala> addTen(6)res2: Int = 16

Combinadores Funcionales

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

scala> numeros.map((i: Int) => i * 3))res1: List[Int] = List(3, 6, 9)

Combinadores Funcionales

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

scala> numeros.partition(_ +1 == 3))res1: (List[Int], List[Int]) = (List(2),List(1, 3))

Tipado estático

Tipado estático

*la inferencia de tipos

Tipado estático*la inferencia de tipos

scala> val x : Int = 1 + 2x: Int = 3

Tipado estático*la inferencia de tipos

scala> val x : Int = 1 + 2x: Int = 3

scala> val x = 1 + 2x: Int = 3

Cuando la inferencia no funciona…

scala> def factorial(n: Int) = { | if (n == 0) 1 | else n * factorial(n – 1) | }

<console>:1: error: recursive method factorial needs result type else n * factorial(n – 1)

^

Cuando la inferencia no funciona…

scala> def factorial(n: Int) = { | if (n == 0) 1 | else n * factorial(n – 1) | }

scala> def factorial(n: Int) : Int = { | if (n == 0) 1 | else n * factorial(n – 1) | }Factorial: (n: Int)Int

orientado a objetos

funcional

objeto-funcional

orientado a objetos

objeto-funcional

funcional

orientado a objetosQué hace

a este lenguaje?

Traits

Colecciones de fields y comportamientos que pueden extend o mixin a sus clases.

trait Pelicula {val tipo: String

}

Traits

trait Pelicula {val tipo: String

}

class Comedia extends Pelicula {val tipo = “comedia”

}

Traits

trait Pelicula {val tipo: String

}

class Comedia extends Pelicula with Tiempo {val tipo = “comedia”val longitud = 90

}

Traits

trait Tiempo {val longitud: Int

}

Modules

Recursos

Scala docs: docs.scala-lang.orgSimply Scala: simplyscala.comScala School: twitter.github.io/scala_school$ sbt console