Search This Blog

Saturday 20 January 2018

BigData - Scala Fundamentals

1.       Scala is functional language. Functions in Scala are similar to objects in Java.
2.       Idiom of Scala is less use of For loop, instead operations are supplied as collections, and those operations are carried out using methods of those collections
3.       Scala encourages immutable data.
4.       Immutable data items can be declared using prefix “val”, similarly mutable data items can be declared using prefix “var”
5.       Expressions are units of code that return a value. Statements are units of code that do not return a value. Expressions can be chained but statements cannot be chained.
6.       Scala favors expression over statements
7.       Any that evaluates to a value is an expression for example string “Hello Word” is an expressions similarly list object like List(“1”,”2”,”a”,”b”) is an expression
8.       When you assign a expression it become statements for example “val t_list = List(“1”,”2”,”a”,”b”)” is a statement does not evaluate to a value. Similarly, statements like printing to screen, writing to file are statements
9.       Expressions can be passed into functions
10.   Multiple expressions can be enclosed into {}, this is called expression block. Last expression in the block serves as return value. Expression blocks can be nested. Expression blocks are nothing but anonymous functions, can have both statements and expressions within block.
11.   If/else, for loop, pattern matching like case or switch are expressions in scala.. these are statements in Java. That is these can be chained in Scala
12.   Functions can be passed in as parameter to other functions and also functions can return functions. Functions can be stored in a variable or a value.
13.   Type inference in scala is done using 3 classes Any, AnyVal, AnyRef. Any is a type that is used for expressions combinations of value type like Int, Double, Char, Boolean  and reference types like Lists
14.   Variables can be used in string interpolation like s”$var1+$var2 is $va3 . i.e. var3 is addition of var1 and var2”
15.   If/else blocks can be used just like other languages
16.   For loop can be written as statements or expressions. If for loops are written like other languages then it’s a statement but if we prefix for block with “yield” keyword then it will become expression and result a List reference type
17.    While loops can not be written as expressions in Scala
18.   Pattern matching is similar to SWITCH/CASE but there is no fall through i.e. only one case will be match therefore break statement not required
a.       Val dayofweek = “Sat”
b.       Val matchday = dayofweek match {
c.                                                                Case “Sat” => “Saturday”
d.                                                                 Case “sun” => “notstatuday”
e.                                                                }
19.   Functions vrs methods
a.       Functions are objects and expression blocks that can be parameterized
b.       Methods on other hand not objects but can be parameterized
c.       Methods must be associated with class but functions can be independent
d.       Method example
                                                               i.      def getArea(radius:Double):Double =
                                                             ii.      {
                                                           iii.          val PI = 3.4
                                                           iv.          PI * radius * radius
                                                             v.      }
                                                           vi.      This above method takes input “radius” or type Double and returns Double
e.       Functions Example
                                                               i.      val getArea = (radius:Double) =>
                                                             ii.      {
                                                           iii.         val PI =3.4
                                                           iv.         PI * radius * raidus
                                                             v.      } :Double
                                                           vi.      This above function takes input “radius” or type Double and returns Double
f.        We can convert method to function as below
                                                               i.      val getvalFunc: (Double) => Double  = getArea
                                                             ii.      or val getvalFunc = getArea (_)
                                                           iii.      _ is a place hold indicating take same type input and return same type as of method.. but place holder does not work for closure where method returns function type but explicit conversion works on closures also
20.   First Order Functions or higher order functions
a.       A function can be stored in a variable or a value
b.       The return value of a function can be a function
c.       A parameter of a function can be a function
21.   Closures
a.       Closures retain the scope of inner function variable even after outer function execution is completed
22.   Collections
a.       Tuples
                                                               i.      Not iterables in Scala.. these are orders collection of items
b.       Lists
                                                               i.      val listval = List(“a”,”A”,”ac”)
                                                             ii.      val listval = “a” :: “A” :: “ac” :: Nil
c.       Maps
                                                               i.      Key value pairs.. defined using “A”=>”va” syntax..
                                                             ii.      These are List of Tuples
d.       Options
                                                               i.      Used for error handling
                                                             ii.      Option with combination of “None” can be used to return None when return type can be returned from function.. example division by zero can return None as it can not return Int or Double
e.       Arrays
                                                               i.      val arranmae = Array(“a”,”A”,”ac”)
                                                             ii.      same as List but items (not length) are mutable
23.   Higher order methods
a.       MAP, FOREACH, FILTER
                                                               i.      Act on one element at a time
b.       SCAN , FOLD, REDUCE

                                                               i.      Act on multiple items at a time

No comments:

Post a Comment