You are on page 1of 27

CISC 886- Cloud and Big Data

Anwar Hossain, Ph.D.


Queen’s University
Email:
ahossain@queensu.ca
anwar.Hossain@gmail.com
Agenda
• scala
Scala
• Scalable Language
• 2003, Martin Odersky, EPFL, Switzerland
• Object oriented and functional language
OOP
Car is a vehicle
Vehicle Sedan is a car
Make Program→ data and operation
Model price

Car Truck

Sedan Suv Pickup Van


ChangePrice()
Functional Programming
• Data and operations are kept separate
• Break up big job to tiny manageable function
• Support for Higher Order Function
• Ideal for data management
• Runs on JVM
Variable definition: Mutable vs immutable
• Mutable: object's state can be changed after it is created
var name: String = "John Doe"
// valid because name is declared as a var (mutable)
name = “Ron Doe"
• Immutable:
• once an object is created, its state cannot be changed
• safer to use in concurrent programming
val name: String = "John Doe"
// compilation error because name is declared as a val (immutable)
name = "Jane Doe"
Control statement
• Control statement
If(a>100)
{
Println(“a>100”)
}
Else
{
Println(“a<100”)
}
Nested control
If(a>100)
{
Println(“a>100”)
}
Else
{
if (a<100)
{
Println(“a<100”)
}
Else
{
Println(“a=100”)
}
}
Block of code to assign single variable value
Scala> Var p =
{
Var x =100
Var y =200
y-x
}
Scala> p (to see the value of p, which is equal to y-x)
Scala> x ➔ error (not in scope)
Scala> var x = 90
Scala>
Var p=
{
X+10
}
Loop
for(input <- 1 to 10)
{
println("current value: " + input)
}

---------------------------------------------------------
(1 to 10).foreach(println)
Nested Loop
for (input <- 1 to 5)
{
for (innerInput <- 1 to 5)
{
println((10*input) + innerInput)
}
}
Loop with two index
for (i <- 1 to 10; j <- 1 to 3) {
println(i + " " + j)
}
Loop: while
Scala> var input =1
Scala>
while(input<5)
{
println(“value: “ + input)
Input = input+1
}
Scala> input <enter>
You see the value of input as 5
Loop: Do….while
Var b = 10
Do
{
println(“value” + b)
}
While (b <10)

➔ Will execute only once for the first time and print “value 10”, then it
will be false at the while condition
Variable with any datatype
Scala> var a = 100
Scala>
var b =
{
If (a==200)
{ 100 }
else
{ “Ahmed” }
}

→ The variable datatype of b will by ‘Any’


Array
Scala> var avar = Array[Int](1,2,3,4,5)
Scala> avar.size <enter>
Scala> avar(avar.size-1)
Scala>
for(input<-avar)
{
println(“current value: “ + input)
}
Variable length array
Scala>
import scala.collection.mutable.ArrayBuffer<enter>
var abvar = new ArrayBuffer[Int]

Scala> abvar.append(100) ➔ adding value to the array


Scala> abvar.append(200)
scala>abvar.append(300)
scala> abvar(0) → getting the first element
Variable length array
Scala>
abvar.remove(0) ➔ removing the first element
Scala>var index = 1
Scala> for (input<-avbar)
{
println(“value of index “ + index + “ is: “ + input
Index = index+1
}
Map – collection of key, value
import scala.collection.mutable.Map<enter>
var mapvar = Map[String, String] (“name”->”ahmed”, “address”->”Egypt”)

Scala> mapvar(“name”) ➔ value of key “name” will be shown


Scala> mapvar.keys
Scala>mapvar.values
Scala>
for (keymapvar.keys)
{
println(“key: “ +key + “ value: “ + mapvar(key))
}
Map
Duplicate key in Map? Not allowed
scala> var mapvar = Map[String, String] (“name”->”ahmed”, “address”-
>”Egypt”, “name”->”Mahmoud”)

Scala> mapvar
Will only see “address” → “Egypt”, “name” → “Mahmoud”
➔ The last value in the key “name” will remain
Tuple
Scala> var empTup = (1234, “John”, 200, “IT”)
➔ Collection of different types
Scala> empTup._1 ➔ see the first element
Scala> empTup_2 → 2nd element
Function
Scala>
def AddTwoNumbers(input1: Int input2: Int):Int =
{ var output = 0
output = input1+input2
output
}

Scala> var sum = AddTwoNoumbers(100, 200)


Reading data from file
import scala.io.Source

object ReadFile {
def main(args: Array[String]): Unit = {
val fileName = "example.txt"
for (line <- Source.fromFile(fileName).getLines()) {
println(line)
}
}
}
Higher order functions
• functions that can
• accept other functions as arguments or
• return a function as a result.

def add(x: Int, y: Int): Int = x + y


def mathOp(f: (Int, Int) => Int, x: Int, y: Int): Int = f(x, y)
val result = mathOp(add, 20, 20)
println(result) // Output: 40
Scala- object oriented approach
class BankAccount {
private var balance = 0.0

def deposit(amount: Double): Unit = {


balance += amount
}

def withdraw(amount: Double): Unit = {


balance -= amount
}

def checkBalance(): Double = {


balance
}
}
Scala- object oriented approach
object Main extends App {
val account = new BankAccount
account.deposit(1000)
account.withdraw(500)
println("Balance: " + account.checkBalance())
}

You might also like