Skip to content

Instantly share code, notes, and snippets.

@thepurpleowl
Last active July 2, 2020 19:27
Show Gist options
  • Save thepurpleowl/047ea4e234e48fd8c9a3a4bc4dbf900c to your computer and use it in GitHub Desktop.
Save thepurpleowl/047ea4e234e48fd8c9a3a4bc4dbf900c to your computer and use it in GitHub Desktop.
Scala Tutorials
val a = 4
var b = 3
// if-else
val greater_4: Boolean = a>b
if(!greater_4)
val asquared = a*a
asquared*2
else
val bsquared = b*b
bsquared*2
// in try-catch-finally finally is always run
// but it'll not affect the returned value,
// so only use of finally is side-effecting
try{
val zero = y/0
}
catch{
case e : ArithmeticException =>0
}
finally{
println("finally always runs but it'll not affect the returned value")
42
}
println("Scala journey starts")
val first: Int = 42
println(s"around the orld in $first days")
/* As Scala is a multi-paradign including functional language,
variables are preferred to be immutable objects to avoid undesirable
change in values of variables.*/
val a = 4
println(f"printing 4 as double $a%.3f")
var b = 3
println(s"sum of two variabes ${a+b}")
// if-else
val greater_4: Boolean = a>b
if(!greater_4)
println("4 is greater")
else
println("3 is greater")
// switch
/*Automatically breaks after the match*/
/*That implies if you put the default case at some not the last option
the control-flow will break there*/
val month="Winter"
month match{
case "Summer" => println("Summer has come")
case "Winter" => println("Winter has come")
case "Autumn" => println("Autumn has come")
case _ => println("option not present")
}
/*That implies if you put the default case at some not the last option
the control-flow will break there*/
val number=420
number match{
case 789 => println("Summer has come")
case 999 => println("Winter has come")
case _ => println("option not present")
case 420 => println("Autumn has come")
}
var s_square = 0
// for
for(x <- 1 to 3){
var squared = x*x
// while
while(squared>0){
s_square += squared
squared -=1;
}
println(s"sum of squares with while $s_square")
squared = x*x
// do while
do{
s_square += squared
squared -=1;
}while(squared>0)
println(s"sum of squares with do-while $s_square")
}
println(s"Final sum of squares $s_square")
//expression
/*{var x=10; x + 20 } - this is an expression
Bcs functional programming paradigm, here the expression has a value, value of the last statement.
This expression has a value 30.*/
println({var x=10; println("winter"); x + 20 })
def squareIt(x: Int) : Int = {
x*x
}
//function can take other function too
def transformer(x:Int, f:Int=>Int):Int={
x*f(x)
}
println(squareIt(9))
println(transformer(9,squareIt))
/*In place of pre defined function we can use "lambda function"/
"function literal", "anonymous function"*/
println(transformer(3,x=>x*7))
println(transformer(12,x=>{val y = x*2;y*y;}))
//To-do : the follwoing is giving undefined in return value in line 30
def transformerMegatron(y:Int) {
try{
val zero = y/0
}
catch{
case e : ArithmeticException =>{
println("Arithmatic exception")
}
}
}
println(transformerMegatron(3))
//tuple
val states = ("UP","AP","Odisha")
//can be accessed with 1 based index
println("State name "+states._3)
val capital = "Odisha"->"Bhubaneswar"
println("Capital of states "+capital._2)
val student = ("Ram",23,"BSc",true)
println(student._4)
//list
val cntry_list = List("India","US","Sweden","Uk")
//can be accessed with 0 based index
println(cntry_list(2))
for(cntry <- cntry_list){
println(cntry)
}
println(cntry_list.head)
println(cntry_list.tail)
for(cntry <- cntry_list.reverse){
println(cntry)
}
//reduce
val n_array = List(1,2,3,4,5)
val sum = n_array.reduce((x : Int, y:Int)=>x+y)
println(sum)
//filter
var hatingtwos = n_array.filter((x:Int)=> x!=2)
println(hatingtwos)
hatingtwos = n_array.filter(_ !=2)
println(hatingtwos)
// More list fun
val numberList = n_array
val reversed = numberList.reverse //> reversed : List[Int] = List(5, 4, 3, 2, 1)
val sorted = reversed.sorted //> sorted : List[Int] = List(1, 2, 3, 4, 5)
val lotsOfDuplicates = numberList ++ numberList //> lotsOfDuplicates : List[Int] = List(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
val distinctValues = lotsOfDuplicates.distinct //> distinctValues : List[Int] = List(1, 2, 3, 4, 5)
val maxValue = numberList.max //> maxValue : Int = 5
val total = numberList.sum //> total : Int = 15
val hasThree = hatingtwos.contains(3) //> hasThree : Boolean = false
val atlas = Map("India"->"NewDelhi","Canada"->"Toronto","UK"->"London")
if(atlas.contains("India")){
println(atlas("India"))
}
val usacapital = util.Try(atlas.get("India")) getOrElse "USA not present in atlas" //check the capital 'O' and 'E'
println(usacapital)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment