Upload
stratio
View
621
Download
3
Tags:
Embed Size (px)
Citation preview
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
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?
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
Functional Programming in Scala
1.1 FP Concepts: Scala is not a purely functional language!!
1 WHY FP?
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
Functional Programming in Scala
2.1 Mutability vs Immutability
2 FP IN SCALA: THE BASICS
Variadic function syntax
Tail call annotation
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 value
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
What else??
Monads!!!
Future
Try
IterateeRDD
(warning!)
...
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
Functional Programming in Scala
Functional way “powers” => Testability, Reusability, Parallelizability,
Comprehension
Immutability & HOFs
For-comprehension => map, flatMap, filter
Functional way => Handling exceptions