Java-1
//class is a blueprint for objects
//create objects from the class blueprint with the keyword new
//
//create objects from the class blueprint with the keyword new
//
import java.io._
class Point(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
}
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
}
object Demo {
def main(args: Array[String]) {
val pt = new Point(10, 20);
def main(args: Array[String]) {
val pt = new Point(10, 20);
// Move to a new location
pt.move(10, 10);
}
}
pt.move(10, 10);
}
}
//Extending a Class
//You can extend a base Scala class and you can design an inherited class in the same way you do it in Java (use extends key word), but there are two restrictions: method overriding requires the override keyword, and only the primary constructor can pass parameters to the base constructor.
//Implicit Classes
//Implicit classes allow implicit conversations with class’s primary constructor when the class is in scope. Implicit class is a class marked with ‘implicit’ keyword.
//Scala is more object-oriented than Java because in Scala, we cannot have static members. Instead, Scala has singleton objects
//A singleton is a class that can have only one instance, i.e., Object
//A private member is visible only inside the class or object that contains the member definition.
//A protected member is only accessible from subclasses of the class in which the member is defined.
//Access modifiers in Scala can be augmented with qualifiers. A modifier of the form private[X] or protected[X] means that access is private or protected "up to" X, where X designates some enclosing package, class or singleton object.
//Methods are implicitly declared abstract if you don’t use the equals sign and the method body.
def functionName ([list of parameters]) : [return type] = {
function body
return [expr]
}
function body
return [expr]
}
object add {
def addInt( a:Int, b:Int ) : Int = {
var sum:Int = 0
sum = a + b
return sum
}
}
def addInt( a:Int, b:Int ) : Int = {
var sum:Int = 0
sum = a + b
return sum
}
}
//A closure is a function, whose return value depends on the value of one or more variables declared outside this function.
val multiplier = (i:Int) => i * 10
var factor = 3
val multiplier = (i:Int) => i * factor
val multiplier = (i:Int) => i * factor
//Arrays
var z:Array[String] = new Array[String](3)
var z:Array[String] = new Array[String](3)
or
var z = new Array[String](3)
// List of Strings
val fruit: List[String] = List("apples", "oranges", "pears")
val fruit: List[String] = List("apples", "oranges", "pears")
// Set of integer type
var s : Set[Int] = Set(1,3,5,7)
var s : Set[Int] = Set(1,3,5,7)
// A map with keys and values.
val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF")
val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF")
val t = new Tuple3(1, "hello", Console)
//An iterator is not a collection, but rather a way to access the elements of a collection one by one. The two basic operations on an iterator it are next and hasNext. A call to it.next()
object Demo {
def main(args: Array[String]) {
val it = Iterator("a", "number", "of", "words")
while (it.hasNext){
println(it.next())
}
}
}
//Pattern matching is the second most widely used feature of Scala, after function values and closures. Scala provides great support for pattern matching, in processing the messages.
def main(args: Array[String]) {
val it = Iterator("a", "number", "of", "words")
while (it.hasNext){
println(it.next())
}
}
}
//Pattern matching is the second most widely used feature of Scala, after function values and closures. Scala provides great support for pattern matching, in processing the messages.
object Demo {
def main(args: Array[String]) {
println(matchTest(3))
}
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
}
def main(args: Array[String]) {
println(matchTest(3))
}
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
}
import scala.util.matching.Regex
object Demo {
def main(args: Array[String]) {
val pattern = "Scala".r
val str = "Scala is Scalable and cool"
println(pattern findFirstIn str)
}
}
def main(args: Array[String]) {
val pattern = "Scala".r
val str = "Scala is Scalable and cool"
println(pattern findFirstIn str)
}
}
//An extractor in Scala is an object that has a method called unapply as one of its members. The purpose of that unapply method is to match a value and take it apart. Often, the extractor object also defines a dual method apply for building values, but this is not required.
//Pattern Matching
object Demo {
def main(args: Array[String]) {
val x = Demo(5)
println(x)
def main(args: Array[String]) {
val x = Demo(5)
println(x)
x match {
case Demo(num) => println(x+" is bigger two times than "+num)
//unapply is invoked
case _ => println("i cannot calculate")
}
}
def apply(x: Int) = x*2
def unapply(z: Int): Option[Int] = if (z%2==0) Some(z/2) else None
}
case Demo(num) => println(x+" is bigger two times than "+num)
//unapply is invoked
case _ => println("i cannot calculate")
}
}
def apply(x: Int) = x*2
def unapply(z: Int): Option[Int] = if (z%2==0) Some(z/2) else None
}
import java.io._
object Demo {
def main(args: Array[String]) {
val writer = new PrintWriter(new File("test.txt" ))
def main(args: Array[String]) {
val writer = new PrintWriter(new File("test.txt" ))
writer.write("Hello Scala")
writer.close()
}
}
writer.close()
}
}
//http://fileadmin.cs.lth.se/scala/scala-impatient.pdf
Comments
Post a Comment