Saturday 4 February 2017

SCALA BASICS Practice on 04 Feb 2017

Scala (Scalable Language)
---------------------------------
1. Object Orieted + Functional Oriented Programming Language
2. In Scala every thing is Object

Java:
----------------
"String" is "immutable"
"StringBuffer & StringBuilder" is "mutable"

"immutable" => we can't change the data
"mutable" => we can change the data


Scala:
-------------------------
val => "value" is "immutable"
var => "variable" is "mutable"


Java Syntax:
----------------------
<data_type> <variable_name> = <data> ;


Scala Syntax:
----------------------
<variable_name> : <data_type> = <data>

val <variable_name> [: <data_type>] = <data>
var <variable_name> [: <data_type>] = <data>




orienit@kalyan:~$ scala
Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_66-internal).
Type in expressions for evaluation. Or try :help.

scala> 



"Scala" Provides 'REPL' promt
R => Read
E => Evaluate
P => Print
L => Loop


"Scala" providing similar to "Python & R" -> "REPL" Promt

"Scala" provides "Type Infer" feature.
Based on the "data" it automatically find the "data type"

scala> val a = 1
a: Int = 1


scala> val a = 1
a: Int = 1

scala> val a = 1.5
a: Double = 1.5

scala> val a = 1.5f
a: Float = 1.5

scala> val a = 1l
a: Long = 1

scala> val a = 1d
a: Double = 1.0

scala> val a = '1'
a: Char = 1

scala> val a = "1"
a: String = 1

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

scala> val a = 1
a: Int = 1

scala> val a : Int = 1
a: Int = 1

scala> val a : Double = 1
a: Double = 1.0

scala> val a : Long = 1
a: Long = 1

scala> val a : Char = 1
a: Char = ?

scala> val a : String = 1
<console>:11: error: type mismatch;
 found   : Int(1)
 required: String
       val a : String = 1
                        ^
------------------------------------------------

scala> val a = 1
a: Int = 1

scala> a.toDouble
res0: Double = 1.0

scala> a.toLong
res1: Long = 1

scala> a.toChar
res2: Char = ?

scala> a.toFloat
res3: Float = 1.0

scala> a.toString
res4: String = 1

------------------------------------------------
"Scala" provides "Operator Overloading" similar to "C++"

scala> val a = 1
a: Int = 1

scala> val b = 2
b: Int = 2

scala> val c = a + b
c: Int = 3

scala> val c = a.+(b)
c: Int = 3


a + b <====>  a.+(b)

a - b <====>  a.-(b)

a * b <====>  a.*(b)

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

scala> val a = 1
a: Int = 1

scala> a = 2
<console>:12: error: reassignment to val
       a = 2
         ^

scala> var a = 1
a: Int = 1

scala> a = 2
a: Int = 2


------------------------------------------------
IF-ELSE
-------------
if(exp) {
body
}

if(exp) {
body1
} else {
body2
}


if(exp1) {
body1
} elseif(exp2) {
body2
} else {
body3
}

------------------------------------------------
Java:
----------
int[] nums = {1, 2, 3, 4, 5};

int[] nums = new int[5];


Scala:
----------
val nums  = Array(1, 2, 3, 4, 5)

val nums  = Array[Int](1, 2, 3, 4, 5)

val nums : Array[Int] = Array[Int](1, 2, 3, 4, 5)


val nums  = Array[Int](10)

val nums  = new Array[Int](10)

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

scala> val nums  = Array[Int](10)
nums: Array[Int] = Array(10)

scala> val nums  = new Array[Int](10)
nums: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

scala> val nums  = Array[Int](1, 2, 3, 4, 5)
nums: Array[Int] = Array(1, 2, 3, 4, 5)

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

val names = Array[String]("anil", "kalyan", "raj", "venkat")

// names[0]

names(0)

------------------------------------------------
scala> val names = Array[String]("anil", "kalyan", "raj", "venkat")
names: Array[String] = Array(anil, kalyan, raj, venkat)

scala> names[0]
<console>:1: error: identifier expected but integer literal found.
names[0]
      ^

scala> names(0)
res5: String = anil

scala> names(0) = "anil kumar"

scala> names
res7: Array[String] = Array(anil kumar, kalyan, raj, venkat)

scala> names = 1
<console>:12: error: reassignment to val
       names = 1
             ^
------------------------------------------------
"Scala" providing 2 types of "collections"
1. immutable collections => scala.collection.immutable
2. mutable collections   => scala.collection.mutable

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

scala> 1 to 10
res8: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> 1 to 10 by 2
res9: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)

scala> 1 to 10 by 3
res10: scala.collection.immutable.Range = Range(1, 4, 7, 10)

scala> 1 until 10
res11: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> 1 until 10 by 2
res12: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)

scala> 1 until 10 by 3
res13: scala.collection.immutable.Range = Range(1, 4, 7)


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

<start number> (to | until) <end number> [by <step number>]

------------------------------------------------
// wrong syntax
for( 1 to 10 ) println(num)

// correct syntax
for( num <- 1 to 10 ) println(num)


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

scala> for( 1 to 10 ) println(num)
<console>:1: error: '<-' expected but ')' found.
for( 1 to 10 ) println(num)
             ^

scala> for( num <- 1 to 10 ) println(num)
1
2
3
4
5
6
7
8
9
10


for( num <- 1 to 10 by  2) println(num)

for( num <- 1 to 10 ) if(num % 2 == 1) println(num)

for( num <- 1 to 10 if(num % 2 == 1) ) println(num)

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

scala> for( num <- 1 to 10 by  2) println(num)
1
3
5
7
9

scala> for( num <- 1 to 10 ) if(num % 2 == 1) println(num)
1
3
5
7
9

scala> for( num <- 1 to 10 if(num % 2 == 1) ) println(num)
1
3
5
7
9


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

for( num <- 1 to 10 ) if(num % 2 == 1) println("num is odd : " + num) else println("num is even : " + num)

scala> for( num <- 1 to 10 ) if(num % 2 == 1) println("num is odd : " + num) else println("num is even : " + num)
num is odd : 1
num is even : 2
num is odd : 3
num is even : 4
num is odd : 5
num is even : 6
num is odd : 7
num is even : 8
num is odd : 9
num is even : 10


------------------------------------------------
String Interpolation:
---------------------------
val id = 1
val name = "kalyan"
val course = "spark"
val percentage = 90.5

val exp1 = "name is kalyan, course is spark"
println(exp1)

val exp2 = "name is " + name + ", course is " + course
println(exp2)

val exp3 = "name is $name, course is $course"
println(exp3)


val exp4 = s"name is $name, course is $course"
println(exp4)


val exp5 = s"name is $name, percentage is $percentage"
println(exp5)


val exp6 = s"name is $name, percentage is $percentage%.2f"
println(exp6)


val exp7 = f"name is $name, percentage is $percentage%.2f"
println(exp7)


val exp8 = s"name is $name\ncourse is $course"
println(exp8)

val exp9 = raw"name is $name\ncourse is $course"
println(exp9)


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




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

scala> val id = 1
id: Int = 1

scala> val name = "kalyan"
name: String = kalyan

scala> val course = "spark"
course: String = spark

scala> val percentage = 90.5
percentage: Double = 90.5

scala> val exp1 = "name is kalyan, course is spark"
exp1: String = name is kalyan, course is spark

scala> println(exp1)
name is kalyan, course is spark


scala> val exp2 = "name is " + name + ", course is " + course
exp2: String = name is kalyan, course is spark

scala> println(exp2)
name is kalyan, course is spark



scala> val exp3 = "name is $name, course is $course"
exp3: String = name is $name, course is $course

scala> println(exp3)
name is $name, course is $course

scala> val exp4 = s"name is $name, course is $course"
exp4: String = name is kalyan, course is spark

scala> println(exp4)
name is kalyan, course is spark


scala> val exp5 = s"name is $name, percentage is $percentage"
exp5: String = name is kalyan, percentage is 90.5

scala> println(exp5)
name is kalyan, percentage is 90.5

scala> val exp6 = s"name is $name, percentage is $percentage%.2f"
exp6: String = name is kalyan, percentage is 90.5%.2f

scala> println(exp6)
name is kalyan, percentage is 90.5%.2f

scala> val exp7 = f"name is $name, percentage is $percentage%.2f"
exp7: String = name is kalyan, percentage is 90.50

scala> println(exp7)
name is kalyan, percentage is 90.50


scala> val exp8 = s"name is $name\ncourse is $course"
exp8: String =
name is kalyan
course is spark

scala> println(exp8)
name is kalyan
course is spark

scala> val exp9 = raw"name is $name\ncourse is $course"
exp9: String = name is kalyan\ncourse is spark

scala> println(exp9)
name is kalyan\ncourse is spark

------------------------------------------------
Functional Programming in Scala:
----------------------------------------
1. Named Functions
2. Anounmus Functions
3. Curried Functions

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

Anounmus Functions:
-----------------------
(x : Int) => { x + 1 }

val add = (x : Int) => { x + 1 }

add(1)

add(10)

------------------------------------------------
scala> (x : Int) => { x + 1 }
res28: Int => Int = <function1>

scala> val add = (x : Int) => { x + 1 }
add: Int => Int = <function1>

scala> add(1)
res29: Int = 2

scala> add(10)
res30: Int = 11


------------------------------------------------
Named Functions:
-----------------------

val add = (x : Int) => { x + 1 }

def add(x : Int) = { x + 1 }

scala> def add(x : Int) = { x + 1 }
add: (x: Int)Int

scala> add(1)
res31: Int = 2

scala> add(10)
res32: Int = 11


------------------------------------------------
Curried Functions:
-----------------------
def add(x : Int, y : Int) = { x + y }

def add(x : Int)(y : Int) = { x + y }


scala> def add(x : Int)(y : Int) = { x + y }
add: (x: Int)(y: Int)Int

scala> add(1)(2)
res33: Int = 3

scala> add(10)(20)
res34: Int = 30

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

val addOne(z : Int) = add(z : Int)(1)
val addOne(z : Int) = add(1)(z : Int)


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

def factorial(n : Int) : Int = {
 if(n == 0) 1
 else n * factorial(n - 1)
}

------------------------------------------------
scala> def factorial(n : Int) : Int = {
     |  if(n == 0) 1
     |  else n * factorial(n - 1)
     | }
factorial: (n: Int)Int


scala> factorial(5)
res38: Int = 120

scala> factorial(4)
res39: Int = 24


def factorial(n : Int) : Int = {
 println(n)
 if(n == 1) 1
 else n * factorial(n - 1)
}

scala> def factorial(n : Int) : Int = {
     |  println(n)
     |  if(n == 1) 1
     |  else n * factorial(n - 1)
     | }
factorial: (n: Int)Int

scala> factorial(5)
5
4
3
2
1
res41: Int = 120

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

def factorial(n : Int) : Int = {
 def fact(x : Int, y: Int) : Int = {
  println(x)
  if(x == y) x
  else x * fact(x + 1, y)
 }
 fact(1,n)
}


scala> def factorial(n : Int) : Int = {
     |  def fact(x : Int, y: Int) : Int = {
     |   println(x)
     |   if(x == y) x
     |   else x * fact(x + 1, y)
     |  }
     |  fact(1,n)
     | }
factorial: (n: Int)Int

scala> factorial(5)
1
2
3
4
5
res44: Int = 120


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

package com.orienit.scala.learnings

case class Student(name: String = "kalyan", id: Int, year: Int)

class Employee(name: String, id: Int)

object Sample3 extends App {
  val s1 = Student("kalyan", 1, 2016)
  val s2 = new Student("venkat", 1, 2016)
  val s3 = new Student(id = 1, year = 2016)
  val s4 = new Student(year = 2016, id = 1)

  val e1 = new Employee("venkat", 1)
  // val e2 = Employee("venkat", 1)

}


Share this article with your friends.

1 comment :

Related Posts Plugin for WordPress, Blogger...