scallla

 in java:

--------------


public static void main (String args[]) {

}


in scala 

def main (args: Array[String]) = {

}


or extends App


App-- traits--holds the main method--helper trait.


app-- helper class.


defaults args,named args, variable length args


-----------------------------


Null

-----------

is a trait is scala.

--one instance of Null and that is null.

--restrict the use of null--as not prefered--leads to null pointer exceptions.

--


Nil

------

empty list


Nothing:

--------------

trait in scala

----no instance of Nothing.


in function when u have error or exception--then return type is nothing


Option:

------


in function u don't any thing valid to return--then

null is not preferred to return--due to null pointer exception.


-it returns some or none.


Unit()

-------------------

type of method which does not return any value.

--its like void in java.


Nothing--there was a error and nothing returned.

unit--side effect


how to deal with nulls in scala code:

--we should not use null.

--can handle with Option.

--can handle none using getOrElse


---------------------------------

yield

-------

 vector--kind of mix of Array and List.

array--index support.

list--immutability.


vector--index based searching+immmutability.


with the help of yield--we will get a new list from for loop.


if guard

----------------


filtering the for loop.


val c= for(i <- 1 to 10; if i%2 == 0) yield {

i*i}

println(c)


pattern guard

-------------------------

case statements--can be combined with if expressions-- to provide extra logic.


def checkSign(x:Int): String = {

x match {

case a if a< 0 => s"$a is a negative no"


}

}

def f(x:Option[Int]) = x match {

case Some(i) if i%2 == 0 => println(i)

case None => prin

case_ => (cover the all scenario)


for comprehension

-------------------------------

for (i<- 1 to 10) println(i*i)


code scala internally converts to

(1 to 10).foreach(i => println((i*i))


for better performance.


for (i<- 1 to 10) yield i*i

converts to

(1 to 10).map(i => i*i)


nested for loop:

for(i<- 1 to 10; j<- 'a' to 'c') yield i*2 +" "+j


difference between == in java and scala:

------------------------------------------------

in Java == meant for reference comparision

equals-- meant for content comparision.


a = new String("Sasmit")

b = new String("Sasmit")


a==b

return false in java--both a and b are different references.


but a.equals(b)---true in java.


in java == is an operator.


in scala everything is scala.


In scala == meant for content comparision.

== same as equals.


eq to compare the references.


strict val vs lazy val

-------------------------------------

val area = { 

println("calculating area" of circle")

3.14 *r*r}


executed immediately--called strict val.


val lazy area = { 

println("calculating area" of circle")

3.14 *r*r}


lazy val--won't executed.


lazy val evaluated in first use.


println(area)


defaults scala packages:

--------------------------


3 packages--implicitly imported for u.


java.lang._

(Math class-- by defaault it comes)

scala._

scala.Predef._


Math.sqrt(16)


java.lang.Math.sqrt(16)


in shell:


java.lang.Math.double tab


scala apply:

--------------------

closes the gap between object oriented and function paradign in scala.


call an object like function


object Person{

def apply(name :String,age:Int) = {

s"$name is $age old"

}

}

Person.apply("Sasmit",30)

Person("Sasmit",30)--object called as function


case class Person(name :String,age:Int)


Diamond Problem in scala andd how to solve:

---------------------------------------

comes with multiple inheritance.


class A{

func1()

}


class B{

funct1()

}


class C extends A,B{

funct1()

}


which function to call--compiler confuse.

so multiple inheritance not supported.


using traits-- its possible


traits--like interfaces--more flexibilities than interfaces--can have implemented and un implemented things..but interfaces--has only unimplemented things.


scala does not support inheritance from multiple class.

user can extends multiple class from traits


linearization rule--come to play to decide hiranchy--right to left


why scala over python or java:

---------------------------------

functional code--concise code--spark writter in scala--new features comes in scala..

scala gives best performance.


type Safe in scala:

-----------------------------

compiler validate types while compiling--throw error--assigning wrong type to variable


val a:Int = "Sasmit"


--------------------

statically typed vs dynamically typed language

--------------------------------------------------

statically type--type of variable known at compile time.

java ,c ,scala.

--give data type.


dyanamically type-- type of variable checked at run time.

e.g.--javascript,python etc.


ststic typing--results --in better performance--as compiler knows data types in advance and scope to do optimization--no chances of error at run time.


----------------------------------

Exception handling in scala:

-----------------------------------

--abnormal condition--becoz of code.

--error--abnormal condition bcoz of system issuues.


try {

  val b = 5/0

}


catch {

case e: Exception => println("please give a denominator other than 0")

}


finally{

println("I will always executed")

}


finally will always executed.


-------------------------------------

monad:

--------------------

--not a class or trait.

--a concept.

--an object--thats wraps another object in scala.

--o/p of a calculation at any step--i/p to other calculation.


val list1 = List(1,2,3,4)

val list2 = List(5,6,7,8)


list1.flatMap { x => list2.map {

y => x+y

}

}


List(List((1+5),(1+6),(1+7),(1+8)),List((2+5),(2+6),(2+7),(2+8))


List(List(6,7,8,9),List(7,8,9,10),List(),List())


List(6,7,8,9,7,8,9,10,..)


---------------------------------

Streams in scala:

---------------------------

--are lazy list in scala

two ways to create a list:


val l = List(1,2,3)


Val l = 100::200::300::Nil


val l = 100 #:: 200 #:: 300 #:: Stream.empty


val l = Stream(1,2,3)


ofDim

------------------------

--used to create a multi dimentional array.


val myarr = Array.oFDim[Int](2,2)


----------------------


Design pattern:

----------------------------

factory design pattern

-------------------------

--separate instance creation logic from client.

--when we have super class with multiple sub class--

--separate class is created where instance is defined.

--offers loose coupling between object creation and client.

--in the factory class-- apply method will be there u need ti pass parameter.

--easy to change object creation logic.


Singleton design pattern:

---------------------------------

--only single instance.

--restrict instantiation of class to one object and provides a global point of access to it.


object Student {

//class level functionality

}


--here already instance is created when u create a object.


Lazy initialization:

--------------------------

it is a technique--initializes value-- on its first access.


-- allows to defer some expensive computation.


val x = {

println("computing x")

}



val x = {

println("computing x")

}


not executed.


difference between Array and ArrayBuffer in scala:

------------------------------------------------

both are mutable.

--array buffer are resizable but Array isn't.

--if u append in ArrayBuffer--it gets larger.

--if u append in Array--it will internally create a new Array.


import scala.collection.mutable.ArrayBuffer


val a = ArrayBuffer(1,2,3,4)


How to remove duplicates from an array or List:

----------------------------------------------

scala> val a = Array(1,2,3,4,4)

a: Array[Int] = Array(1, 2, 3, 4, 4)


scala> a.toSet

res0: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)


scala> a.toSet.toArray

res1: Array[Int] = Array(1, 2, 3, 4)


scala> val a = Array(1,2,3,4,4)

a: Array[Int] = Array(1, 2, 3, 4, 4)


scala> a.toSet

res0: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)


scala> a.toSet.toArray

res1: Array[Int] = Array(1, 2, 3, 4)



we can use toSet.toArray to remove duplicate or distinct.


same for List.


----------------------------


cntrl+shift+f-- to format in eclipse.

Comments

Popular posts from this blog

scala-4