Another episode in series of Functional Thinking in Scala. This episode relates to Referential Transparency, Substitution model, context dependence and error handling in Scala.
4. (c) 2014-15 Knoldus Software 4
impure
Scala is an impure functional programming
language
5. (c) 2014-15 Knoldus Software 5
no side effects
Mutation
I/O
Exceptions
printing to console
as well
6. (c) 2014-15 Knoldus Software 6
side-effect
If the function does something other than just providing
result
➔
Modifying a variable
➔
Modifying a data structure in place
➔
Setting a field on an object
➔
Throwing an exception or halting with an error
➔
Printing to the console or reading user input
➔
Reading from or writing to a file
➔
Drawing on the screen
7. (c) 2014-15 Knoldus Software 7
benefits of a pure function
Easy to
Test
Reuse
Parallelise
Generalize
Reason
8. (c) 2014-15 Knoldus Software 8
a pure function
●
A function f with input type A and output type B (written in Scala as
a single type: A => B , pronounced “ A to B ” or “ A arrow B ”) is a
computation that relates every value a of type A to exactly one
value b of type B such that b is determined solely by the value of a .
●
Any changing state of an internal or external process is irrelevant
to computing the result f(a)
●
A function intToString having type Int => String will take every
integer to a corresponding string. Furthermore, if it really is a
function, it will do nothing else
9. (c) 2014-15 Knoldus Software 9
a pure function
Referential
Transparency
Substitution
Model
Not context
dependent
11. (c) 2014-15 Knoldus Software 11
referential transparency
What is the difference between 2 code blocks?
12. (c) 2014-15 Knoldus Software 12
referential transparency
What is the difference between 2 code blocks?
If the expression can be replaced by its value and
vice versa AND nothing changes then it is called
Referential Transparency.
This model of substituting values for expressions
is called Substitution Model.
14. (c) 2014-15 Knoldus Software 14
substitution model
What do you expect the output to be?
15. (c) 2014-15 Knoldus Software 15
substitution model
What do you expect the output to be?
16. (c) 2014-15 Knoldus Software 16
substitution model
Now, let us replace money2 with referntial transparency. Hence money2 becomes
Money1.add(20)
Your sum1 is Money(60)
Your sum2 is Money(90)
Why?
17. (c) 2014-15 Knoldus Software 17
substitution model
Ok, let us change the way Money works
What do you expect the output to be?
18. (c) 2014-15 Knoldus Software 18
substitution model
Ok, let us change the way Money works
What do you expect the output to be?
Your sum1 is GoodMoney(40)
Your sum2 is GoodMoney(40)
19. (c) 2014-15 Knoldus Software 19
substitution model
And now lets apply the referential transparency
What do you expect the output to be?
24. (c) 2014-15 Knoldus Software 24
Exceptions are not type safe
What is the datatype of this ?
How would the caller know that there is an exception which can occur?
How was it better in Java?
25. (c) 2014-15 Knoldus Software 25
Exception break RT
Exceptions introduce context dependence
What would be the output?
What would be the output once we introduce RT?
26. (c) 2014-15 Knoldus Software 26
Exception break RT
After introducing RT
What would be the output?
28. (c) 2014-15 Knoldus Software 28
sentinel value
●
Return a Sentinel value
Sentinel value
Silent error propagation
Boilerplate code
Typed methods would not know value
Demands special handling at caller end
29. (c) 2014-15 Knoldus Software 29
other options?
Explicit return that function may not always have
a value
What would you do?
30. (c) 2014-15 Knoldus Software 30
other options?
Explicit return that function may not always have
a value
What would you do? Option
31. (c) 2014-15 Knoldus Software 31
option
When exception could be thrown, wrap the
result in an Option
32. (c) 2014-15 Knoldus Software 32
wrapping exception throwing
api's
●
Lifting methods
f(A=>B)
def lift[A,B](f: A => B): Option[A] => Option[B] = _ map f
Example : val k = lift(math.abs)
33. (c) 2014-15 Knoldus Software 33
wrapping exception throwing
api's
●
Any number of parameters
35. (c) 2014-15 Knoldus Software 35
return exceptions
Sometimes we do want to tell the caller that an
exception has occured and pass the exception
36. (c) 2014-15 Knoldus Software 36
return exceptions
Sometimes we do want to tell the caller that an
exception has occured and pass the exception
Either
37. (c) 2014-15 Knoldus Software 37
Let us wrap our method
println(divideWithError(10,1).fold(a=>a,b=>b+1))
println(divideWithError(10,0).fold(a=>a,b=>b+1))