FP in Scala
Enero 2015
WHY FP?
• FP Concepts
• FP “powers”
1 2 3FP IN SCALA: THE BASICS
• Mutability vs
Immutability
• HOFs
• Functions = First-c...
WHY FP?
1
Functional Programming in Scala
1.1 FP Concepts: Pure Function
A function of type A => B maps
every value of type A to exa...
Functional Programming in Scala
1.1 FP Concepts: Referential transparency
1 WHY FP?
If you have an expression, this expres...
Functional Programming in Scala
1.1 FP Concepts: Scala is not a purely functional language!!
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Testability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Testability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Testability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Testability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Reusability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Reusability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Reusability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Reusability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Reusability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Parallelizability
1 WHY FP?
Functional Programming in Scala
1.2 FP “Powers”: Comprehension
1 WHY FP?
FP IN SCALA:
THE BASICS2
Functional Programming in Scala
2.1 Mutability vs Immutability
2 FP IN SCALA: THE BASICS
Functional Programming in Scala
2.1 Mutability vs Immutability
2 FP IN SCALA: THE BASICS
Functional Programming in Scala
2.1 Mutability vs Immutability
2 FP IN SCALA: THE BASICS
Functional Programming in Scala
2.1 Mutability vs Immutability
2 FP IN SCALA: THE BASICS
All the classes that mix-in this ...
Functional Programming in Scala
2.1 Mutability vs Immutability
2 FP IN SCALA: THE BASICS
Variadic function syntax
Tail cal...
Functional Programming in Scala
2.2 Higher Order Functions
2 FP IN SCALA: THE BASICS
Functional Programming in Scala
2.3 Functions = First-class citizens
2 FP IN SCALA: THE BASICS
Method
Function
Functional Programming in Scala
2.3 Functions = First-class citizens
2 FP IN SCALA: THE BASICS
Function literal
Function v...
FP TOOLS
3
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
flatMap
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
http://docs.scala-lang.org/tutorials/FAQ/yield.html
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
Functional Programming in Scala
3.1 For-comprehension
3 FP TOOLS
What else??
Monads!!!
Future
Try
Iteratee
RDD
(warning!)
...
Functional Programming in Scala
3.2 Try & Either
3 FP TOOLS
Alternatives to “pure” exceptions
Why?
1º. Exceptions break RT...
Functional Programming in Scala
3.2 Try & Either
3 FP TOOLS
Functional Programming in Scala
3.2 Try & Either
3 FP TOOLS
//Awesome Dataviiiiiis code ;)
Functional Programming in Scala
3.2 Try & Either
3 FP TOOLS
//Awesome Dataviiiiiis code ;)
Functional Programming in Scala
3.2 Try & Either
3 FP TOOLS
Functional Programming in Scala
Functional Programming in Scala
Functional way “powers” =>
Testability, Reusability, Parallelizability,
Comprehension
Immu...
Functional programming in scala
Próxima SlideShare
Cargando en…5
×

Functional programming in scala

1.752 visualizaciones

Publicado el

Publicado en: Tecnología
0 comentarios
4 recomendaciones
Estadísticas
Notas
  • Sé el primero en comentar

Sin descargas
Visualizaciones
Visualizaciones totales
1.752
En SlideShare
0
De insertados
0
Número de insertados
31
Acciones
Compartido
0
Descargas
27
Comentarios
0
Recomendaciones
4
Insertados 0
No insertados

No hay notas en la diapositiva.

Functional programming in scala

  1. 1. FP in Scala Enero 2015
  2. 2. WHY FP? • FP Concepts • FP “powers” 1 2 3FP IN SCALA: THE BASICS • Mutability vs Immutability • HOFs • Functions = First-class citizens FP TOOLS • For-comprehension (desugaring) • Try & Either ÍNDICE
  3. 3. WHY FP? 1
  4. 4. Functional Programming in Scala 1.1 FP Concepts: Pure Function A function of type A => B maps every value of type A to exactly one value of type B And does nothing else “Nothing else” = Side effects: Modifying a data structure, setting a field on a object, throwing an exception, printing to the console, reading from or writing to a file 1 WHY FP?
  5. 5. Functional Programming in Scala 1.1 FP Concepts: Referential transparency 1 WHY FP? If you have an expression, this expression is RT if you can evaluate it and then replace this expression with the value that you get A function is pure if f(x) is RT when x is RT
  6. 6. Functional Programming in Scala 1.1 FP Concepts: Scala is not a purely functional language!! 1 WHY FP?
  7. 7. Functional Programming in Scala 1.2 FP “Powers”: Testability 1 WHY FP?
  8. 8. Functional Programming in Scala 1.2 FP “Powers”: Testability 1 WHY FP?
  9. 9. Functional Programming in Scala 1.2 FP “Powers”: Testability 1 WHY FP?
  10. 10. Functional Programming in Scala 1.2 FP “Powers”: Testability 1 WHY FP?
  11. 11. Functional Programming in Scala 1.2 FP “Powers”: Reusability 1 WHY FP?
  12. 12. Functional Programming in Scala 1.2 FP “Powers”: Reusability 1 WHY FP?
  13. 13. Functional Programming in Scala 1.2 FP “Powers”: Reusability 1 WHY FP?
  14. 14. Functional Programming in Scala 1.2 FP “Powers”: Reusability 1 WHY FP?
  15. 15. Functional Programming in Scala 1.2 FP “Powers”: Reusability 1 WHY FP?
  16. 16. Functional Programming in Scala 1.2 FP “Powers”: Parallelizability 1 WHY FP?
  17. 17. Functional Programming in Scala 1.2 FP “Powers”: Comprehension 1 WHY FP?
  18. 18. FP IN SCALA: THE BASICS2
  19. 19. Functional Programming in Scala 2.1 Mutability vs Immutability 2 FP IN SCALA: THE BASICS
  20. 20. Functional Programming in Scala 2.1 Mutability vs Immutability 2 FP IN SCALA: THE BASICS
  21. 21. Functional Programming in Scala 2.1 Mutability vs Immutability 2 FP IN SCALA: THE BASICS
  22. 22. Functional Programming in Scala 2.1 Mutability vs Immutability 2 FP IN SCALA: THE BASICS All the classes that mix-in this trait should be in this file A is covariant
  23. 23. Functional Programming in Scala 2.1 Mutability vs Immutability 2 FP IN SCALA: THE BASICS Variadic function syntax Tail call annotation
  24. 24. Functional Programming in Scala 2.2 Higher Order Functions 2 FP IN SCALA: THE BASICS
  25. 25. Functional Programming in Scala 2.3 Functions = First-class citizens 2 FP IN SCALA: THE BASICS Method Function
  26. 26. Functional Programming in Scala 2.3 Functions = First-class citizens 2 FP IN SCALA: THE BASICS Function literal Function value
  27. 27. FP TOOLS 3
  28. 28. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  29. 29. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  30. 30. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  31. 31. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  32. 32. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  33. 33. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  34. 34. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS flatMap
  35. 35. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS http://docs.scala-lang.org/tutorials/FAQ/yield.html
  36. 36. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  37. 37. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  38. 38. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  39. 39. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS
  40. 40. Functional Programming in Scala 3.1 For-comprehension 3 FP TOOLS What else?? Monads!!! Future Try Iteratee RDD (warning!) ...
  41. 41. Functional Programming in Scala 3.2 Try & Either 3 FP TOOLS Alternatives to “pure” exceptions Why? 1º. Exceptions break RT 2º. Exceptions are NOT type-safe
  42. 42. Functional Programming in Scala 3.2 Try & Either 3 FP TOOLS
  43. 43. Functional Programming in Scala 3.2 Try & Either 3 FP TOOLS //Awesome Dataviiiiiis code ;)
  44. 44. Functional Programming in Scala 3.2 Try & Either 3 FP TOOLS //Awesome Dataviiiiiis code ;)
  45. 45. Functional Programming in Scala 3.2 Try & Either 3 FP TOOLS
  46. 46. Functional Programming in Scala
  47. 47. Functional Programming in Scala Functional way “powers” => Testability, Reusability, Parallelizability, Comprehension Immutability & HOFs For-comprehension => map, flatMap, filter Functional way => Handling exceptions

×