Created
November 1, 2018 11:45
-
-
Save adityajn105/f1e2386d0cebc9f00e25b813b00e2342 to your computer and use it in GitHub Desktop.
A tutorial to quickly revise Scala syntax and features
This file contains hidden or 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
/* | |
* Scala - Scalable Language, hybrid functional Programming language | |
* Integrates features of OOP and functional languages | |
* Compliled on JVM | |
* Can use all classes of JAVA SDK and also own custom classes | |
* Some different features from JAVA | |
* 1. All types are objects | |
* 2. Tpye inference | |
* 3. Nested Functions | |
* 4. Functions are also objects (like python) | |
* 5. Closures (like python) - a function whose return value depends on value of one ore more variables declared outside this function. | |
*/ | |
object HelloWorld { | |
def main(args: Array[String]) { | |
//declare variables - mutable | |
var myVar : String = "First variable" | |
//declare values - immutable | |
val myVal : String = "First value" | |
//type inference | |
var a = 7 | |
var b = 6L | |
var c = 6.76 | |
var d = 6.98F | |
var ans = """ | |
multiline string | |
a is int, b is long, c is double, d is float | |
""" | |
println(ans) | |
//declaring a tuple - immutable | |
var tup = (40,"hello") | |
println(tup._1) | |
//multiple assignments using tuple | |
val (v1:Int , v2) = (40,42.7F) | |
println(v1,v2) | |
//creating objects | |
var point = new Point(30,50) | |
point.move(2,4) | |
point.move(3,-1) | |
//for is different | |
var x = 0 | |
for(x <- 1 to 10){ print(x+" ") } | |
println() | |
for(x <- 1 until 10){ print(x+" ") } | |
println() | |
val numList = List(5,6,7) | |
//nested loop using for | |
for(a <- 1 to 3; b <- numList ){ | |
print((a,b)+" ") | |
} | |
println() | |
//for with yield | |
var retVal = for( x <- numList | |
if x %3 != 0; if x < 10 | |
) yield x | |
println(retVal) | |
} | |
} | |
//class name works as class constructor | |
class Point(val xc:Int, val yc:Int){ | |
var x = xc | |
var y:Int = yc | |
def move(dx:Int, dy:Int){ | |
x = x+dx | |
y = y+dy | |
println("Current position: "+(x,y)) | |
} | |
} |
This file contains hidden or 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
class ThreeDPoint(override val xc: Int, override val yc: Int, val zc: Int) extends Point(xc,yc){ | |
var z : Int = zc | |
//if function do not return anything use Unit | |
//procedure | |
def move(dx: Int, dy: Int, dz: Int) : Unit = { | |
x+=dx | |
y+=dy | |
z+=dz | |
println("Current position: "+(x,y,z)) | |
} | |
//function | |
def getPosition(): (Int, Int, Int) = { | |
return (x,y,z) | |
} | |
} | |
//scalas singleton object | |
object InheritenceExample { | |
def main(args: Array[String]){ | |
var tdp = new ThreeDPoint(10,20,30) | |
tdp.move(1,2,3) | |
tdp.move(3,4,5) | |
print("I am at "+tdp.getPosition()) | |
} | |
} | |
/* | |
* Private members are visible only inside the class or object | |
* Protected member is only accessible from subclass of the class in which member is defined | |
* Public member can be accessed from everywhere; default | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment