Scala(Scalable Language)
---------------------------------------------
Scala is Functional + Object Oriented Programing Language
Scala everything (function / method / class / object / variable ) is Object only
Java (Primitive data types / Wrapper Classes / Objects / Interfaces )
---------------------------------------------
Java:
-----------
int a = 1;
String name = "kalyan";
Mutable / Immutable data types
String is immutable
StringBuffer, StringBuilder are mutable
<data type> <varaible name> = <data>
---------------------------------------------
Scala:
-----------------
val => value is immutable (we can't change the data)
var => variable is mutable (we can change the data)
val <variable name > [ : <data type> ] = <data>
var <variable name > [ : <data type> ] = <data>
---------------------------------------------
Scala support command line feature (REPL)
R => Read
E => Evaluate
P => Print
L => Loop
To start the scala => 'scala' is the command
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 `Type Infer`
---------------------------------------------
scala> val name = "kalyan"
name: String = kalyan
scala> val name : String = "kalyan"
name: String = kalyan
scala> name = "xyz"
<console>:12: error: reassignment to val
name = "xyz"
^
---------------------------------------------
scala> var name = "kalyan"
name: String = kalyan
scala> var name : String = "kalyan"
name: String = kalyan
scala> name = "xyz"
name: String = xyz
---------------------------------------------
scala> var id = 1
id: Int = 1
scala> var id : Int = 1
id: Int = 1
scala> var id = 1l
id: Long = 1
scala> var id : Long = 1
id: Long = 1
scala> var id = 1d
id: Double = 1.0
scala> var id : Double = 1
id: Double = 1.0
scala> var id = 1f
id: Float = 1.0
scala> var id : Float = 1
id: Float = 1.0
scala> var id : Char = 1
id: Char = ?
scala> var id : Char = 2
id: Char = ?
scala> var id : Char = 3
id: Char = ?
scala> var id : Char = 311
id: Char = ķ
scala> var id : Char = 97
id: Char = a
scala> var id : String = 97
<console>:11: error: type mismatch;
found : Int(97)
required: String
var id : String = 97
^
scala> var id : String = "97"
id: String = 97
scala> var id : String = 97.toString
id: String = 97
---------------------------------------------
scala> var id = 1
id: Int = 1
scala> 1
res0: Int = 1
scala> 1
res1: Int = 1
scala> 1 + 2
res2: Int = 3
scala> val sum = 1 + 2
sum: Int = 3
---------------------------------------------
Java don't have operator overloading
C++ / Scala have operator overloading
---------------------------------------------
scala> var a = 1
a: Int = 1
scala> var b = 2
b: Int = 2
scala> var c = a + b
c: Int = 3
scala> var c = a.+(b)
c: Int = 3
scala> var c = a - b
c: Int = -1
scala> var c = a.-(b)
c: Int = -1
explanation:
---------------
a + b <==> a.+(b)
---------------------------------------------
; importance:
------------------
scala> var a = 1 var b = 1
<console>:1: error: ';' expected but 'var' found.
var a = 1 var b = 1
^
scala> var a = 1 ; var b = 1
a: Int = 1
b: Int = 1
---------------------------------------------
scala> var id = 1
id: Int = 1
scala> var x = id.toLong
x: Long = 1
scala> var x = id.toDouble
x: Double = 1.0
scala> var x = id.toFloat
x: Float = 1.0
scala> var x = id.toString
x: String = 1
scala> var x = id.toByte
x: Byte = 1
scala> var x = id.toChar
x: Char = ?
---------------------------------------------
isInstanceOf and asInstanceOf :
----------------------------------
scala> var id = 1
id: Int = 1
scala> id.isInstanceOf[Int]
res3: Boolean = true
scala> id.isInstanceOf[Long]
res4: Boolean = false
scala> id.asInstanceOf[Long]
res5: Long = 1
scala> var id : Long = 100
id: Long = 100
scala> id.asInstanceOf[Long]
res6: Long = 100
scala> id.asInstanceOf[Int]
res7: Int = 100
scala> id.asInstanceOf[String]
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
... 32 elided
---------------------------------------------
IF / IF-ELSE / IF-ELSEIF
---------------------------------------------
scala> if(true) { println("kalyan") }
kalyan
scala> if(false) { println("kalyan") }
scala> var id = 5
id: Int = 5
scala> if(id == 5) { println("kalyan") }
kalyan
scala> if(id != 5) { println("kalyan") }
scala> if(id > 4) { println("kalyan") }
kalyan
scala> if(id > 4) { println("kalyan") } else { println("xyz")}
kalyan
scala> if(id > 6) { println("kalyan") } else { println("xyz")}
xyz
---------------------------------------------
scala> var a = 5
a: Int = 5
scala> var b = "hi"
b: String = hi
scala> var c = true
c: Boolean = true
if(a > 5) {
println(a)
} else if (b.contains("a")) {
println(b)
} else if (!c) {
println(c)
} else {
println("Not found")
}
scala> if(a > 5) {
| println(a)
| } else if (b.contains("a")) {
| println(b)
| } else if (!c) {
| println(c)
| } else {
| println("Not found")
| }
Not found
---------------------------------------------
Arrays in Scala:
------------------------
scala> var nums = Array(1,2,3,4,5)
nums: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var nums = Array[Int](1,2,3,4,5)
nums: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var nums : Array[Int] = Array[Int](1,2,3,4,5)
nums: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var nums = Array(5)
nums: Array[Int] = Array(5)
scala> var nums = new Array(5)
nums: Array[Nothing] = Array(null, null, null, null, null)
scala> var nums = new Array[Int](5)
nums: Array[Int] = Array(0, 0, 0, 0, 0)
---------------------------------------------
scala> var nums = Array(1,2,3,4,5)
nums: Array[Int] = Array(1, 2, 3, 4, 5)
scala> nums[0]
<console>:1: error: identifier expected but integer literal found.
nums[0]
^
scala> nums(0)
res17: Int = 1
scala> nums(1)
res18: Int = 2
scala> nums(1) = 11
scala> nums
res20: Array[Int] = Array(1, 11, 3, 4, 5)
---------------------------------------------
collections in scala:
-------------------------
1. mutable collection => scala.collection.mutable
2. immutable collection => scala.collection.immutable
---------------------------------------------
scala> 1 to 10
res22: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> 1 to 10 by 2
res23: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
scala> 1 to 10 by 3
res24: scala.collection.immutable.Range = Range(1, 4, 7, 10)
scala> 1 to 10 by 4
res25: scala.collection.immutable.Range = Range(1, 5, 9)
scala> 1 to 10
res26: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> 1 until 10
res27: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
---------------------------------------------
For loop in scala:
------------------------
scala> for(1 to 10) println(x)
<console>:1: error: '<-' expected but ')' found.
for(1 to 10) println(x)
^
scala> for(x <- 1 to 10) println(x)
1
2
3
4
5
6
7
8
9
10
scala> for(x <- 1 to 10) if(x % 2 == 1) println(x)
1
3
5
7
9
scala> for(x <- 1 to 10 if x % 2 == 1) println(x)
1
3
5
7
9
scala> for(x <- 1 to 10 if x % 2 == 1 and x > 5) println(x)
<console>:14: error: value and is not a member of Boolean
for(x <- 1 to 10 if x % 2 == 1 and x > 5) println(x)
^
scala> for(x <- 1 to 10 if x % 2 == 1 && x > 5) println(x)
7
9
---------------------------------------------
collections in scala:
-------------------------
1. mutable collection => scala.collection.mutable
2. immutable collection => scala.collection.immutable
var arr = Array[Int](1,2,3,4,5)
var list = List[Int](1,2,3,4,5)
var set = Set[Int](1,2,3,4,5)
var seq = Seq[Int](1,2,3,4,5)
var vec = Vector[Int](1,2,3,4,5)
var queue = Queue[Int](1,2,3,4,5)
var stack = Stack[Int](1,2,3,4,5)
---------------------------------------------
scala> var arr = Array[Int](1,2,3,4,5)
arr: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var list = List[Int](1,2,3,4,5)
list: List[Int] = List(1, 2, 3, 4, 5)
scala> var set = Set[Int](1,2,3,4,5)
set: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)
scala> var seq = Seq[Int](1,2,3,4,5)
seq: Seq[Int] = List(1, 2, 3, 4, 5)
scala> var vec = Vector[Int](1,2,3,4,5)
vec: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4, 5)
scala> var queue = Queue[Int](1,2,3,4,5)
<console>:11: error: not found: value Queue
var queue = Queue[Int](1,2,3,4,5)
^
scala> var queue = scala.collection.immutable.Queue[Int](1,2,3,4,5)
queue: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3, 4, 5)
scala> var queue = scala.collection.mutable.Queue[Int](1,2,3,4,5)
queue: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5)
---------------------------------------------
scala> var list = List[Int](4,5,6)
list: List[Int] = List(4, 5, 6)
scala> var list = (4 :: 5 :: 6 :: Nil)
list: List[Int] = List(4, 5, 6)
scala> var list = (4 :: (5 :: (6 :: Nil)))
list: List[Int] = List(4, 5, 6)
scala> var list = List[Int](4,5,6)
list: List[Int] = List(4, 5, 6)
scala> list = 3 :: list
list: List[Int] = List(3, 4, 5, 6)
scala> list = 3 :: list :: 7
<console>:12: error: value :: is not a member of Int
list = 3 :: list :: 7
^
---------------------------------------------
scala> var list = List[Int](4,5,6)
list: List[Int] = List(4, 5, 6)
scala> list = 3 +: list
list: List[Int] = List(3, 4, 5, 6)
scala> list = list :+ 7
list: List[Int] = List(3, 4, 5, 6, 7)
scala> list = 2 +: list :+ 8
list: List[Int] = List(2, 3, 4, 5, 6, 7, 8)
---------------------------------------------
scala> var list1 = List[Int](4,5,6)
list1: List[Int] = List(4, 5, 6)
scala> var list2 = List[Int](7,8,9)
list2: List[Int] = List(7, 8, 9)
scala> var list3 = list1 :: list2
list3: List[Any] = List(List(4, 5, 6), 7, 8, 9)
scala> var list3 = list1 ::: list2
list3: List[Int] = List(4, 5, 6, 7, 8, 9)
scala> var list = List(1, 1.0, true, "hi")
list: List[Any] = List(1, 1.0, true, hi)
---------------------------------------------
Functions in Scala:
-------------------------
1. anonymous functions
2. named functions
3. curried functions
Anonymous functions:
------------------------
(x : Int) => { x + 1 }
scala> (x : Int) => { x + 1 }
res33: Int => Int = <function1>
scala> val add = (x : Int) => { x + 1 }
add: Int => Int = <function1>
scala> add(10)
res34: Int = 11
Named functions:
--------------------
def add(x : Int) = { x + 1 }
scala> def add(x : Int) = { x + 1 }
add: (x: Int)Int
scala> def add(x : Int) : Int = { x + 1 }
add: (x: Int)Int
scala> def add(x : Int) : Long = { x + 1 }
add: (x: Int)Long
scala> add(10)
res35: Long = 11
---------------------------------------------
scala> def add(x : Int) = { x + 1 }
add: (x: Int)Int
scala> add(10)
res36: Int = 11
scala> def add(x : Int) = { var y = x + 1 ; println(y)}
add: (x: Int)Unit
scala> add(10)
11
scala> def add(x : Int) = { var y = x + 1 ; y}
add: (x: Int)Int
scala> add(10)
res38: Int = 11
---------------------------------------------
scala> def add(x : Int) = { var y = x + 1 ; if(y > 1) y else x}
add: (x: Int)Int
scala> def add(x : Int) = { var y = x + 1 ; if(y > 1) y else "not found"}
add: (x: Int)Any
scala> def add(x : Int) = { var y = x + 1 ; if(y > 1) y.toString else "not found"}
add: (x: Int)String
---------------------------------------------
Recurrsive Functions in Scala:
------------------------------------
def factorial(n : Int) : Int = {
var res = 1
for(x <- 1 to n) res = res * x
res
}
scala> def factorial(n : Int) : Int = {
| var res = 1
| for(x <- 1 to n) res = res * x
| res
| }
factorial: (n: Int)Int
scala> factorial(5)
res39: Int = 120
scala> factorial(6)
res40: Int = 720
def factorial(n : Int) : Int = {
if(n == 1) 1
else n * factorial(n - 1)
}
scala> def factorial(n : Int) : Int = {
| if(n == 1) 1
| else n * factorial(n - 1)
| }
factorial: (n: Int)Int
scala> factorial(5)
res41: Int = 120
scala> factorial(6)
res42: Int = 720
---------------------------------------------
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(4)
4
3
2
1
res43: Int = 24
---------------------------------------------
def factorial(n : Int) : Int = {
def fact(start: Int, end: Int) : Int = {
println(start)
if(start == end) end
else start * fact(start + 1, end)
}
fact(1,n)
}
scala> def factorial(n : Int) : Int = {
| def fact(start: Int, end: Int) : Int = {
| println(start)
| if(start == end) end
| else start * fact(start + 1, end)
| }
| fact(1,n)
| }
factorial: (n: Int)Int
scala> factorial(4)
1
2
3
4
res46: Int = 24
Note: Tail-Recursion use @tailrec anotation
---------------------------------------------
Curried Functions:
--------------------
def add(a : Int, b : Int) : Int = { a + b }
def add(a : Int)(b : Int) : Int = { a + b }
---------------------------------------------
scala> def add(a : Int, b : Int) : Int = { a + b }
add: (a: Int, b: Int)Int
scala> add(10,20)
res47: Int = 30
scala> def add(a : Int)(b : Int) : Int = { a + b }
add: (a: Int)(b: Int)Int
scala> add(10)(20)
res48: Int = 30
---------------------------------------------
def add(a : Int)(b : Int) : Int = { a + b }
def addOne(x : Int) : Int = add(x)(1)
def addTwo(x : Int) : Int = add(x)(2)
def addAny(x : Int) : Int = add(10)_
---------------------------------------------
scala> def addOne(x : Int) : Int = add(x)(1)
addOne: (x: Int)Int
scala> def addTwo(x : Int) : Int = add(x)(2)
addTwo: (x: Int)Int
---------------------------------------------
def add(a : Int, b : Int) : Int = { a + b }
def add(a : Int, b : Int = 5) : Int = { a + b }
def add(a : Int = 1, b : Int = 5) : Int = { a + b }
---------------------------------------------
scala> def add(a : Int = 1, b : Int = 5) : Int = { a + b }
add: (a: Int, b: Int)Int
scala> add()
res51: Int = 6
scala> add(2)
res52: Int = 7
scala> add(2,3)
res53: Int = 5
scala> add(b = 2)
res54: Int = 3
scala> add(b = 2, a = 1)
res55: Int = 3
---------------------------------------------
Java => Scala
---------------------------------------
Interface => Trait
Abstract Class => Abstract Class
Class => Class & Case Class
=> object
---------------------------------------------
String Interpolation:
----------------------------
var a = 1
var b = 10.5
var c = "kalyan"
var exp1 = "a = $a , b = $b , c = $c"
println(exp1)
var exp2 = s"a = $a , b = $b , c = $c"
println(exp2)
var exp3 = s"a = $a , b = $b%.2f , c = $c"
println(exp3)
var exp4 = f"a = $a , b = $b%.2f , c = $c"
println(exp4)
var exp5 = s"a = $a\nb = $b\nc = $c"
println(exp5)
var exp6 = raw"a = $a\nb = $b\nc = $c"
println(exp6)
---------------------------------------------
scala> var a = 1
a: Int = 1
scala> var b = 10.5
b: Double = 10.5
scala> var c = "kalyan"
c: String = kalyan
scala>
scala> var exp1 = "a = $a , b = $b , c = $c"
exp1: String = a = $a , b = $b , c = $c
scala> println(exp1)
a = $a , b = $b , c = $c
scala> var exp2 = s"a = $a , b = $b , c = $c"
exp2: String = a = 1 , b = 10.5 , c = kalyan
scala> println(exp2)
a = 1 , b = 10.5 , c = kalyan
scala> var exp3 = s"a = $a , b = $b%.2f , c = $c"
exp3: String = a = 1 , b = 10.5%.2f , c = kalyan
scala> println(exp3)
a = 1 , b = 10.5%.2f , c = kalyan
scala> var exp4 = f"a = $a , b = $b%.2f , c = $c"
exp4: String = a = 1 , b = 10.50 , c = kalyan
scala> println(exp4)
a = 1 , b = 10.50 , c = kalyan
scala> var exp5 = s"a = $a\nb = $b\nc = $c"
exp5: String =
a = 1
b = 10.5
c = kalyan
scala> println(exp5)
a = 1
b = 10.5
c = kalyan
scala> var exp6 = raw"a = $a\nb = $b\nc = $c"
exp6: String = a = 1\nb = 10.5\nc = kalyan
scala> println(exp6)
a = 1\nb = 10.5\nc = kalyan
---------------------------------------------
Share this article with your friends.
Well explained Blog
ReplyDeleteWe are making the Best Software training in bangalore.
Software Training Institute in Bangalore
Hadoop Training in Bangalore