Powered By Blogger

Sunday, November 10, 2019

Scala contd 2



What is scala ?
Svcala is hybrid programming langauage which supports both object oriented programming and functional
aspect. As data engineers we are more concernted about functional programming.


Why scala?
Concise langauage, which gives functional aspect. Provides scripting approach, so we get better
productivity. Spark is written in scala. Data crunching can be achieved using scala

REPL vs IDE ?


what is fuctional programming?
fuctional programming is a way of writing software  using pure functions and immutable values.



function :  function is a relationship between input and output.
ex: squareroot of 4 is 2.

What is Pure function :
It has 3 characterics,
1. function should be dependent on only the input parameter. Any external variabe should
   not imapact the results.
 
    def func(i:Int) = {
        2*i
    }

2. pure function should not modify the input parameter value.
        def func(i:Int) = {
            2*i
        }

3. There should not be any side effects.
    function should do only what is intended to do.

    def doubler(x:Int){
        println("hello this is a double function")
        2*i
    }
here we should not have even println statement.



Purity of function is tested using referencial transperency.
referencial transperency :
If we can replace the occurrence of the function with exact output,as the output does
not change. and it should give same result.


=========================================================================================
first class function.

whatever we can do with values samething can be done by function.
like
    1. can pass value to a variable. Similarly function can be assigned and called
        var a = 5
        def doubler(a: Int) = { a*2}
        var i = doubler(_)
        i(2)

            scala> def func(i:Int) = {
                |         2*i
                |     }
            func: (i: Int)Int

            scala>         def doubler(a: Int) = { a*2}
            doubler: (a: Int)Int

            scala>         var i = doubler(_)
            i: Int => Int =

            scala> i(2)
            res28: Int = 4



2. function can itself be passed as parameter to other function. We have seen

3. Function can return another function.

So functions are called as first class entities.

=========================================================================================
Higher order function

Either function takes function as parameter or function returns another function. That function
is called as Higher order function.

=========================================================================================
anonymus function

its the inline function . Function will not have any name.

transformInt(3, x=>x*x*x)

=========================================================================================
Immutability

We cannot mutate/change the values.

=========================================================================================

Loop vs recursion vs tail recursion

calculating factorial using loop

    def factorial(x: Int) ={
        var result = 1
        for(i <- 1="" p="" to="" x="">            result = result * i
        }
        result
    }


    scala> def factorial(x: Int) ={
     |         var result = 1
     |         for(i <- 1="" p="" to="" x="">     |             result = result * i
     |         }
     |         result
     |     }
factorial: (x: Int)Int

scala> factorial(4)
res30: Int = 24


factorial using recursion

def factorialRecursion(x: Int) : Int ={
    if (x >= 1){
        x * factorialRecursion(x-1)
    }else{
        1
    }

}


def factorialRecursion(x: Int) : Int ={
    if (x == 1){
       1
    }else{
        x*factorialRecursion(x-1)
    }

}

factorialRecursion(4)


scala> def factorialRecursion(x: Int) : Int ={
     |     if (x == 1){
     |        1
     |     }else{
     |         x*factorialRecursion(x-1)
     |     }
     |
     | }
factorialRecursion: (x: Int)Int

scala>

scala> factorialRecursion(4)
res32: Int = 24

4*factorialRecursion(3)
4*3*factorialRecursion(2)
4*3*2*factorialRecursion(1)
4*3*2*1

As the expansion happens it will eat the stack mempry.


=========================================================================================
tail recursion


def factorialTailRecursion(x:Int, result : Int) : Int ={
    factorialTailRecursion(x-1, x*result)
    result
}

factorialTailRecursion(5,1)


terminating condition will be mentioned at the startig.


tail recursion will not occupy the memory, as it will have one entry in stack


=========================================================================================
statements vs Expressions

var a = println("hello")
println(a)

scala> var a = println("hello")
hello
a: Unit = ()

scala> println(a)
()


Every code in scala is returning something. In case of println , returns ()

var a = 5
var result = if(a==5) 6 else 7
println(result)

scala> var a = 5
a: Int = 5

scala> var result = if(a==5) 6 else 7
result: Int = 6

scala> println(result)
6

even the if statement is also returning the value. so it is an expression

Expression is something whihc returns value.
<- 1="" p="" to="" x=""><- 1="" p="" to="" x="">
<- 1="" p="" to="" x=""><- 1="" p="" to="" x="">
statements is any code of line.
expression returns some value. In scala each line of code returns some value
so it is called as Expression language.



function taking another function as input
<- 1="" p="" to="" x=""><- 1="" p="" to="" x="">
def function(i : Int)= (i:int) ==>{i*i}

No comments:

Post a Comment