Transition from java OOPs to scala FP -2
Welcome to part 2 of the series . As promised , I am back again with continued story of my transition from java to scala . Believe me , I don’t want anyone to go through the same long painful journey which i did during learning of this niche paradigm of programming . I want to simplify your life to such an extent that you understand FP to the max and you are well versed to read / understand any Scala code and make required changes to it or write your own ones .If you haven’t already gone through my introduction blog , i would strongly recommend you to do so. Link here:
For convenience , i have tried to broadly classify the topics into following sections :
- Introduction to functions ,different ways of representing them and implicits .
- Designing your own system in Scala ( would constitute the building blocks like case classes , traits , anonymous functions and others ) .
- Currying , Partial Functions and Higher Order Functions .
- Scala collections : Use cases and advanced features .
- Recursion .
- Cats Library :why , when , which and how to use it ( Would include Monoids , Functors , Monads , M Transformers , different flavors of monads in cats library) .
- Asynchronous and synchronous processing in Scala .
Without wasting much of time , lets begin with these inspiring words :
“ The beginning is the most important part of the work.”
Functions :
Functions are the first class citizens in FP (Functional Programming , i would be using FP in most of my talks) . In FP they are just like algebraic functions . If you have a little bit knowledge of set theory or what mapping means you would easily understand what a function is .
In mathematics , y=f(x) would mean y is a function of x . This implies y can be any thing that varies over x for e.g y=2x i.e y=2*x (Double function) . Implied , if you have value of x as 2 the value of y would be 4 and so on ..
Similarly , In scala a function (pure function , i would come to it in a while ) is a mapping of Input => Output i.e for any input , we should have an output that doesn't change for a fixed input.So, the same Double function above in mathematics would look like below in scala :
def double(value: Int): Int = {
value * 2
}
I won’t guide you through scala syntax , as i assume you are are familiar with basics of it . Some properties can be noted down for the above function :
- It is very much similar to the algebraic function y=f(x)=x*2 as both doubles the input value .{scala pure functions have algebraic behavior }
- Input and Output values are deterministic i.e for a fixed input you will always get a fixed output .{Known range of values for a known domain}
- there is no talking to external world ,i.e. it just cares about its input and doubles it ,which is its output . { referred as it has No Side Effects }
- We can substitute value of the function with their corresponding values i.e double(5) can be substituted by 10 . {Known as Referential Transparency}
So folks , all functions in scala that have above four properties are called as pure functions . In our discussion we would mostly stick to pure functions unless we are bound to use impure ones . There is one more way you can represent this double function i.e
val double = (value: Int) => 2 * value
This is known as anonymous function way . call it by double(16) .Just to clarify the above function is an instance of type Function1 which takes in one input and returns single output .
With that said , I would like to take a pause here and expect y'all to play around with above concepts . See if you can reason about your functions as pure ones , if not why ? can you make them pure somehow ? . hmm , i know you will ask why do i care about making them pure if my task is getting done !— Well the answer is not single lined . I would get back with all your impatient queries in the next part of the series . Till then enjoy this quote from Masterminds of Programming , by Federico Biancuzzi -:)