Powered By Blogger

Sunday, November 10, 2019

Scala basisc

language basics of spark
var vs val
var : is the variable.
val is fixed.

var a=5
a=7
is possible

val b= 5
b = 7

scala> val b= 5
b: Int = 5

scala> b = 7
:27: error: reassignment to val
         b = 7
           ^


We use var mostly. Use val as it gives immutable.


By default type is inferred, though we dont specify types.

val a : Int = 5

scala> val a : Int = 5
a: Int = 5

scala> val b =5
b: Int = 5

Boolean, Char, Double, Float, Long , Byte are the supported data types.

printing something
println("here is output")


scala> println("here is output")
here is output

In JAVA we have switch, similarly we case match. in switch we have to give break, in scall
we dont need break.

case _ == > default match

//for x in 1 to 10
for (x <-- 10="" 1="" nbsp="" p="" to="">    val squared = x* x
    println(squared)
}

we have while and do while loop in scala


===============
Expressions
===============
Always the last line of the statement will be returned from the expression.
"Returns" the final value in a block.

{ val x =10; x+20; 7}

scala> { val x =10; x+20; 7}
res3: Int = 7


{ val x =10; x+20}

scala> { val x =10; x+20}
res4: Int = 30


println({ val x =10; x+20})
scala> println({ val x =10; x+20})
30


===============
Functional programming
===============

As data engineers we are more worried about functional programming.
We should know how to create the functions not to worry about oops.


def squareIt(x: Int) : Int ={
    x*x
}

println(squareIt(4))


scala> def squareIt(x: Int) : Int ={
     |     x*x
     | }
squareIt: (x: Int)Int

scala>

scala> println(squareIt(4))
16

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

def cubeIt(x: Int) : Int ={x*x*x}
println(cubeIt(2))

---

def transformInt(x:Int, f:Int=> Int) : Int = {
    f(x)
}

here we can give f, but same name should be given for f(x) any name.

val result = transformInt(2, squareIt)
println(result)


scala> def transformInt(x:Int, f:Int=> Int) : Int = {
     |     f(x)
     | }
transformInt: (x: Int, f: Int => Int)Int

scala> val result = transformInt(2, squareIt)
result: Int = 4

scala> println(result)
4

Higher order function


function can take input as function. Here squareIt is the function invoked

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

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

Here we are passing anonymus function.

scala> transformInt(3, x=>x*x*x)
res11: Int = 27

def fivideByTwo(x: Int)={
    x/2
}

transformInt(10, x=>x/2)

transformInt(2, x=> {val y = x*2;y*y})


scala> transformInt(3, x=>x*x*x)
res11: Int = 27

scala> transformInt(2, x=> {val y = x*2;y*y})
res12: Int = 16

scala> def fivideByTwo(x: Int)={
     |     x/2
     | }
fivideByTwo: (x: Int)Int

scala> transformInt(10, x=>x/2)
res13: Int = 5

scala> transformInt(2, x=> {val y = x*2;y*y})
res14: Int = 16


=======================
tuple

tuple can hold elements of different data types. Its the light weight collection,
can be thought as A ROW


val captainStuff = ("Picard","Enterprise-D","NCC-1701-D")
println(captainStuff)
println(captainStuff._1)

Index is 1 based in tuple


scala> val captainStuff = ("Picard","Enterprise-D","NCC-1701-D")
captainStuff: (String, String, String) = (Picard,Enterprise-D,NCC-1701-D)

scala> println(captainStuff)
(Picard,Enterprise-D,NCC-1701-D)

scala> println(captainStuff._1)
Picard
























No comments:

Post a Comment