REPL – ( Read Evaluate Print Loop Shell ) , read the value ,evaluate it and reprinted as confirmation
scala> 1+1
res0: Int = 2
Numeric Data types —-> Int , Long ,Double ,Float,Byte and Short
Value vs Variable – Value which will referred as immutable object and Variable will be referred as mutable object . Let see the definition one by one
We have created new variable as “ samplevar ” which will be assigned as integer and the result obtained as 11 and we can reassigned with new amount as 9.But if we tried to assign the string value as “change Int to String ” to samplevar .It will throws error as mention below
scala> var samplevar = 5+6
samplevar: Int = 11
scala> samplevar = 4+5
samplevar: Int = 9
scala> samplevar = ” change Int to String “
:21: error: type mismatch;
found : String(” change Int to String “)
required: Int
samplevar = ” change Int to String “
^
Even Values are immutable as like Variable we cannot change the datatype but here case is we can change the value in run time
scala> val sampleVal = ” assigned values”
sampleVal: String = ” assigned values”
scala> sampleVal = ” tried to reassigned values”
:21: error: reassignment to val
sampleVal = ” tried to reassigned values”
^
String Interpolation – It’s used to build the string based values and scala notation will be “s” prefix added before the variable
scala> var totalcount = 458
totalcount: Int = 458
scala> s” Total ${totalcount} of them completed scala training”
res4: String = ” Total 458 of them completed scala training”
In Scala an object can be thought of as a structure that encapsulates some data and functions . here we are creating object as ScalaObject with some function as sorted ,addition,forloop,switchcase and stringoperation
object ScalaObject{
def testforsorted(args: Array[String])={
val unsorted = List(9,3,1,0,7,5,9,3,11)
print(“sorted: ” + unsorted.sorted)
}
def testforaddition(x:Int, y:Int) = {
println(“addition: ” + (x+y))
}
def testforLoop( args:String* ) = {
var count : Int = 0;
for( arg
println(“For Loop[” + count + “] = ” + arg );
count = count + 1;
}
}
def testforStringOPeration( strOpr:String ) = {
println(“String for reverse —–>” + strOpr.reverse );
println(“String for capitalize—–> ” + strOpr.capitalize );
}
def testforswitch( checkweekdays:String ) = {
checkweekdays match
{
case “MON” | “TUE” |”WED” |”THU” |”FRI” => print(” Working day “)
case “SAT” | “SUN” => print(” Weekend have a party !!! “)
}
}
}
Results
scala> ScalaObject.testforsorted( Array() );
sorted: List(0, 1, 3, 3, 5, 7, 9, 9, 11)
scala>
scala>
scala> ScalaObject.testforaddition(50,20);
addition: 70
scala>
scala> ScalaObject.testforLoop(“Scala”,”REPL”,”BIG DATA”,”Apache Spark”);
For Loop[0] = Scala
For Loop[1] = REPL
For Loop[2] = BIG DATA
For Loop[3] = Apache Spark
scala> ScalaObject.testforStringOPeration(“Scala learning”);
String for reverse —–>gninrael alacS
String for capitalize—–> Scala learning
scala> ScalaObject.testforswitch(“MON”);
Working day
scala> ScalaObject.testforswitch(“SUN”);
Weekend have a party !!!
scala>
Simple Multiplication table using while loop function
object ScalaMultiplication {
def TableObj(tableNo:Int) = {
var sum = 0
println(tableNo+” Times Table”)
while ( sum < 10) {
sum+=1
println(sum + ” X ” + tableNo + ” = ” + ( tableNo*sum ) )
}
}
}
Result :
scala> ScalaMultiplication.TableObj(5);
5 Times Table
1 X 5 = 5
2 X 5 = 10
3 X 5 = 15
4 X 5 = 20
5 X 5 = 25
6 X 5 = 30
7 X 5 = 35
8 X 5 = 40
9 X 5 = 45
10 X 5 = 50
Collections – Scala collection has higher order collections like map ,filter and reduce which will help to manipulate the data in very short and fast way . Let see one by one
List
scala> var places = List(“chennai”,”pune”,”Banglore”,”Delhi”,”Mumbai”)
places: List[String] = List(chennai, pune, Banglore, Delhi, Mumbai)
head and tail method will used to pull the collection i.e first and remaning values from the list
scala> places.head
res5: String = chennai
scala> places.tail
res6: List[String] = List(pune, Banglore, Delhi, Mumbai)
scala> places(1);
res7: String = pune
scala> places(3);
res8: String = Delhi
for loop using places list
scala> places.foreach( ( pal :String ) => println( pal ) )
chennai
pune
Banglore
Delhi
Map : – Map is a collection of key value pair and values are using keys
scala> val placesMap = Map((“chennai” -> “TN”), (“pune” -> “MAH”), (“hyderabad” -> “AP”) ,(“patna” -> “bihar”) )
placesMap: scala.collection.immutable.Map[String,String] = Map(chennai -> TN, pune -> MAH, hyderabad -> AP, patna -> bihar)
scala> placesMap(“chennai”);
res7: String = TN
scala> placesMap.keys.foreach{ i => print( “Mapper Capital = ” + i ); println(” Mapper State = ” + placesMap(i) ); } ;
Mapper Capital = chennai Mapper State = TN
Mapper Capital = pune Mapper State = MAH
Mapper Capital = hyderabad Mapper State = AP
Mapper Capital = patna Mapper State = bihar
Classes : –
scala> class employee( name:String , sal:Int ) {
| println(name + ” working as data scientist and his salary ” + sal )
| }
defined class employee
scala> new employee (“william” , 2500)
william working as data scientist and his salary 2500