Last active
December 12, 2018 08:33
-
-
Save unnisworld/bbc0bcec182040eb13d11bd0502b1b8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object HelloWorld { | |
def main(args: Array[String]) { | |
println("Hello Scala!") | |
// Writing scala applications | |
// The entry point of a scala application is a main method with | |
// this signature - def main(args: Array[String]) | |
// The declaration "object HelloWorld" is a way to create Signleton objects | |
// in scala. You will learn more about that later. | |
// Another way to create application is to extend App | |
// main method will be inherited from App | |
// object Main extends App { | |
// Console.log("Hello World") | |
// } | |
// scala class definition | |
class MyClass { | |
private var myField = 0; | |
def getMyField() : Int = { | |
return this.myField; | |
} | |
def addToMyField(value : Int) { | |
this.myField += value; | |
} | |
} | |
val myClassInstance1 = new MyClass() | |
println(myClassInstance1.getMyField()) | |
myClassInstance1.addToMyField(1) | |
println(myClassInstance1.getMyField()) | |
// Difference between val and var | |
val v1 = 1 | |
// v1 cannot be reassigned as it is a const | |
// uncommenting below statement can cause compilation error | |
// v1 = 2 | |
// var's can be reassigned | |
var v2 = 2 | |
v2 = 3 | |
// scala allows "+" "-" "++" etc to be used as method names | |
// In below example "+" is a method on Int | |
var v3 = 3.+(4) | |
// We can write it this way also because scala supports infix notation as well | |
// if ( flight1.conflictsWith(flight2) ) can be written as | |
// if ( flight1 conflictsWith flight2 ) | |
v3 = 1 + 2 | |
// If statements | |
// If statements are expressions in scala, meaning they can return a value | |
var myInt : Int = 1; | |
var myText : String = | |
if(myInt == 0) "myInt == 0"; | |
else "myInt != 0"; | |
println(myText); | |
// For loops | |
// The i <- construct is called a generator. | |
// The "1 to 10" is actually a method call that returns the Range type. | |
// "1 to 10" is same as (1).to(10); | |
for(i <- 1 to 10) { | |
println("i is " + i); | |
} | |
// to vs until for Range types | |
// prints only till 9 | |
for(i <- 1 until 10) { | |
println("i is " + i); | |
} | |
// Filtering for loops | |
var myArray1 : Array[String] = new Array[String](10); | |
myArray1(0) = "1111" | |
myArray1(0) = "1115" | |
for(value : String <- myArray1 if value.endsWith("5")) { | |
println(value); | |
} | |
// Introduction to List | |
val animals = List("rabbit", "lion", "tiger", "deer") | |
println(animals) | |
val bigAnimals = animals.map(_.toUpperCase()) | |
println(bigAnimals) | |
val someMoreAnimals = List("Goat", "Cow", "Sheep") | |
val allAnimals = animals ++ someMoreAnimals | |
println(allAnimals) | |
val listOfNums = List(1,2,3) | |
val doubledList = listOfNums.map(_ * 2) | |
println(doubledList); | |
val doubledList2 = listOfNums.map(x => x*2) | |
println(doubledList2); | |
val filteredList = doubledList.filter(_ > 4) | |
println(filteredList) | |
// Introduction to Arrays | |
// Arrays are immutable objects | |
// Once created length cannot be changed | |
var myArray : Array[String] = new Array[String](2); | |
myArray(0) = "some value"; | |
println(myArray(0)) | |
// Iterating using array index | |
for(i <- 0 until myArray.length) { | |
println("i is: " + i); | |
println("i'th element is: " + myArray(i)); | |
} | |
for(myString <- myArray) { | |
println(myString); | |
} | |
// Immutable class definition | |
case class Point(x:Int, y:Int) { | |
} | |
val p1 = new Point(1,2) | |
val p2 = new Point(1,2) | |
// case class vs class | |
// For a case class Compiler generates following methods | |
// extractor, equals, hashcode, copy | |
println(p1.equals(p2)) | |
// Mutable class definition | |
// Notice the introduction of "var" keyword | |
case class MutablePoint(var x:Int, var y:Int) { | |
} | |
val p3 = new MutablePoint(1,2) | |
p3.x = 5 | |
println(p3.x) | |
// case class pattern matching example | |
case class Person(firstName : String, lastName : String) { | |
} | |
val person1 = new Person("John", "Doe") | |
val Person(firstName, lastName) = person1 | |
println(firstName, lastName) | |
// Default args and named params | |
case class Polygon() { | |
def plot(color:String = "black", | |
symbol:String = "", | |
lineStyle:String = "solid", | |
width:Int = 1) | |
{ | |
println(s"Plotting polygon with color: $color," + | |
s"symbol: $symbol, style: $lineStyle, width: $width") | |
} | |
} | |
val polygon1 = new Polygon() | |
polygon1.plot(); | |
// calling with named arguments | |
polygon1.plot(color="red", symbol="~") | |
// Singletons and companion objects | |
// Singleton pattern in Java is replaced by Object declaration | |
// Singletons can be created using declaration! | |
object MySingleton { | |
def foo() { | |
println(s"Foo is invoked $this") | |
} | |
} | |
MySingleton.foo() | |
// Companion objects | |
// Scala cannot have static methods and variables | |
// Use companion objects instead | |
// The class definition and companion object definition should be in the | |
// same file. | |
class Main { | |
def sayHelloWorld() { | |
println("Hello World"); | |
} | |
} | |
object Main { | |
def sayHi() { | |
println("Hi!"); | |
} | |
} | |
var aMain : Main = new Main(); | |
aMain.sayHelloWorld(); | |
// This call is like static method invocation in Java | |
Main.sayHi(); | |
// try-catch-finally | |
try | |
{ | |
throwsException(); | |
} | |
catch { | |
case e: IllegalArgumentException => println("illegal arg. exception"); | |
case e: IllegalStateException => println("illegal state exception"); | |
} | |
finally { | |
println("this code is always executed"); | |
} | |
//A METHOD THAT THROWS EXCEPTION | |
def throwsException() { | |
throw new IllegalStateException("Exception thrown"); | |
} | |
// scala match expressions | |
// They are similar to switch statements in Java but has some differences | |
// 1. won't follow through after a match. No need of break statement. | |
// 2. supports many data types | |
// 3. can return a value | |
var myVar1 = "theValue"; | |
myVar1 match { | |
case "someValue" => println(myVar1 + " 1"); | |
case "thisValue" => println(myVar1 + " 2"); | |
case "theValue" => println(myVar1 + " 3"); | |
case "doubleValue" => println(myVar1 + " 4"); | |
} | |
// eg: of match expession returning a value | |
var myVar2 = "theValue"; | |
var myResult = | |
myVar2 match { | |
case "someValue" => myVar2 + " A"; | |
case "thisValue" => myVar2 + " B"; | |
case "theValue" => myVar2 + " C"; | |
case "doubleValue" => myVar2 + " D"; | |
} | |
println(myResult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment