Pf con scala
-
Upload
rebeccasliter -
Category
Documents
-
view
1.377 -
download
1
description
Transcript of Pf con scala
![Page 1: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/1.jpg)
PFRebecca Sliter, ThoughtWorks @rebeccasliter
con
Scala
![Page 2: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/2.jpg)
utiliza el JVM
![Page 3: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/3.jpg)
static-typed
utiliza el JVM
![Page 4: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/4.jpg)
static-typed
orientado a objetos
utiliza el JVM
![Page 5: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/5.jpg)
static-typed
orientado a objetos?
utiliza el JVM
![Page 6: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/6.jpg)
static-typed
orientado a objetos
utiliza el JVM
![Page 7: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/7.jpg)
static-typed
funcional
orientado a objetos
utiliza el JVM
![Page 8: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/8.jpg)
static-typed
funcional
orientado a objetos
objeto-funcional
utiliza el JVM
![Page 9: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/9.jpg)
funcionalQué hacea este lenguaje?
![Page 10: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/10.jpg)
Funciones de orden superior
Funciones que tienen otra función como parametro
![Page 11: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/11.jpg)
Funciones de orden superior
Funciones que tienen otra función como parametro
Funciones que retornan una función
![Page 12: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/12.jpg)
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.”}
![Page 13: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/13.jpg)
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.”}
![Page 14: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/14.jpg)
Evaluación estricta
scala> val a = b + 1; val b = 2;a: Int = 1b: Int = 2
![Page 15: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/15.jpg)
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
![Page 16: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/16.jpg)
Currying
scala> def add(a: Int)(b: Int) = a + badd: (a: Int)(b: Int)Int
![Page 17: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/17.jpg)
Currying
scala> def add(a: Int)(b: Int) = a + badd: (a: Int)(b: Int)Int
scala> add(5)(6)res1: Int = 11
![Page 18: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/18.jpg)
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>
![Page 19: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/19.jpg)
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
![Page 20: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/20.jpg)
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)
![Page 21: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/21.jpg)
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))
![Page 22: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/22.jpg)
Tipado estático
![Page 23: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/23.jpg)
Tipado estático
*la inferencia de tipos
![Page 24: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/24.jpg)
Tipado estático*la inferencia de tipos
scala> val x : Int = 1 + 2x: Int = 3
![Page 25: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/25.jpg)
Tipado estático*la inferencia de tipos
scala> val x : Int = 1 + 2x: Int = 3
scala> val x = 1 + 2x: Int = 3
![Page 26: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/26.jpg)
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)
^
![Page 27: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/27.jpg)
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
![Page 28: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/28.jpg)
orientado a objetos
funcional
objeto-funcional
![Page 29: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/29.jpg)
orientado a objetos
objeto-funcional
funcional
![Page 30: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/30.jpg)
orientado a objetosQué hace
a este lenguaje?
![Page 31: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/31.jpg)
Traits
Colecciones de fields y comportamientos que pueden extend o mixin a sus clases.
![Page 32: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/32.jpg)
trait Pelicula {val tipo: String
}
Traits
![Page 33: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/33.jpg)
trait Pelicula {val tipo: String
}
class Comedia extends Pelicula {val tipo = “comedia”
}
Traits
![Page 34: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/34.jpg)
trait Pelicula {val tipo: String
}
class Comedia extends Pelicula with Tiempo {val tipo = “comedia”val longitud = 90
}
Traits
trait Tiempo {val longitud: Int
}
![Page 35: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/35.jpg)
Modules
![Page 36: Pf con scala](https://reader034.fdocuments.net/reader034/viewer/2022051400/54c421d84a7959385e8b460e/html5/thumbnails/36.jpg)
Recursos
Scala docs: docs.scala-lang.orgSimply Scala: simplyscala.comScala School: twitter.github.io/scala_school$ sbt console