Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Thinking functional-in-scala

1,317 views

Published on

Another episode in series of Functional Thinking in Scala. This episode relates to Referential Transparency, Substitution model, context dependence and error handling in Scala.

Published in: Technology
  • Be the first to comment

Thinking functional-in-scala

  1. 1. 1 Thinking Functional in Scala Vikas Hazrati Knoldus Software LLP
  2. 2. (c) 2014-15 Knoldus Software 2 agenda ● Introduction ● Referential Transparency ● Substitution Model ● Exception Handling
  3. 3. (c) 2014-15 Knoldus Software 3 agenda ● Introduction ● Referential Transparency ● Substitution Model ● Exception Handling
  4. 4. (c) 2014-15 Knoldus Software 4 impure Scala is an impure functional programming language
  5. 5. (c) 2014-15 Knoldus Software 5 no side effects Mutation I/O Exceptions printing to console as well
  6. 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. 7. (c) 2014-15 Knoldus Software 7 benefits of a pure function Easy to Test Reuse Parallelise Generalize Reason
  8. 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. 9. (c) 2014-15 Knoldus Software 9 a pure function Referential Transparency Substitution Model Not context dependent
  10. 10. (c) 2014-15 Knoldus Software 10 agenda ● Introduction ● Referential Transparency ● Substitution Model ● Exception Handling
  11. 11. (c) 2014-15 Knoldus Software 11 referential transparency What is the difference between 2 code blocks?
  12. 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.
  13. 13. (c) 2014-15 Knoldus Software 13 agenda ● Introduction ● Referential Transparency ● Substitution Model ● Exception Handling
  14. 14. (c) 2014-15 Knoldus Software 14 substitution model What do you expect the output to be?
  15. 15. (c) 2014-15 Knoldus Software 15 substitution model What do you expect the output to be?
  16. 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. 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. 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. 19. (c) 2014-15 Knoldus Software 19 substitution model And now lets apply the referential transparency What do you expect the output to be?
  20. 20. (c) 2014-15 Knoldus Software 20 another example
  21. 21. (c) 2014-15 Knoldus Software 21 another example Ideally we should be able to call any listIngredients with coffee
  22. 22. (c) 2014-15 Knoldus Software 22 side effect What is the side effect here ?
  23. 23. (c) 2014-15 Knoldus Software 23 Functional Exception Handling
  24. 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. 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. 26. (c) 2014-15 Knoldus Software 26 Exception break RT After introducing RT What would be the output?
  27. 27. (c) 2014-15 Knoldus Software 27 How should we work with Exceptions?
  28. 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. 29. (c) 2014-15 Knoldus Software 29 other options? Explicit return that function may not always have a value What would you do?
  30. 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. 31. (c) 2014-15 Knoldus Software 31 option When exception could be thrown, wrap the result in an Option
  32. 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. 33. (c) 2014-15 Knoldus Software 33 wrapping exception throwing api's ● Any number of parameters
  34. 34. (c) 2014-15 Knoldus Software 34 converting non option methods
  35. 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. 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. 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))
  38. 38. (c) 2014-15 Knoldus Software 38 That's It ! ;)

×