Greetings Leb Geeks ,
So i thought of exposing this one useful subject to ones that aren't aware of/ familiar with , as well as making bold out of this subject and luring in programmers attentions towards this brilliant functional and scalable yet closureful programming language, which will most probably be viewed as a highly interesting language to Java Gurus specifically , The Scala Lang.
Prior to a brief overview about Scala , i'd like to say that this OOP language is created by Martin Odersky the designer of Generic Java, and the builder of the current generation of javac , so this might mean something to a few..
Fast Definition
Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application.
Top Advantages of Scala Over Java
1 - Very nice Closures
val belowFirst = ( xs : List[Int] ) => {
val first = xs( 0 )
val isBelow = ( y : Int ) => y < first
for( x <- xs; if( isBelow( x ) ) yield x
}
2 - Code size reduced by 30% to 50%
Java Example:
int n = 0;
for (String s: array) {
if (s.length > 2 && mapping.containsKey(s)) n++;
}
String[] bigEnough = new String[n];
n = 0;
for (String s: array) {
if (s.length <= 2) continue;
bigEnough[n++] = map.get(s);
}
Corresponding Scala example:
val bigEnough = array.filter(_.length > 2).flatMap(mapping.get)
3 - Functional
def qsort: List[Int] => List[Int] = {
case Nil => Nil
case pivot :: tail =>
val (smaller, rest) = tail.partition(_ < pivot)
qsort(smaller) ::: pivot :: qsort(rest)
}
4 - Variances , goes in Scala as follows:
class InVar[T] { override def toString = "InVar" }
class CoVar[+T] { override def toString = "CoVar" }
class ContraVar[-T] { override def toString = "ContraVar" }
/************ Regular Assignment ************/
val test1: InVar[String] = new InVar[String]
val test2: CoVar[String] = new CoVar[String]
val test3: ContraVar[String] = new ContraVar[String]
5 - Upper and Lower type bounds
type parameters and abstract types may be constrained by a type bound. Such type bounds limit the concrete values of the type variables and possibly reveal more information about the members of such types (Upper) or limit a type to a subtype of another type, lower type bounds declare a type to be a supertype of (Lower)
6 - Compound Types
def cloneAndReset(obj: Cloneable with Resetable): Cloneable = {
//...
}
7 - Multiple values return methods using tuple
tuple example:
val tuple = ("Nader", 30, "Beiruth")
tuple match {
case (name, age, city) =>{
println("Name: " + name)
println("Age: " + age)
println("City: " + city)
}
}
Fast Scala Code Examples
casual HelloWorld:
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
For - yield loop
object Main {
def main(args: Array[String]) {
val res = for (a <- args) yield a.toUpperCase
println("Arguments: " + res.toString)
}
}
Easy Matching
object Main {
var verbose = false
def main(args: Array[String]) {
for (a <- args) a match {
case "-h" | "-help" =>
println("Usage: scala Main [-help|-verbose]")
case "-v" | "-verbose" =>
verbose = true
case x =>
println("Unknown option: '" + x + "'")
}
if (verbose)
println("How are you today?")
}
}
Maps
object Maps {
val colors = Map("red" -> 0xFF0000,
"turquoise" -> 0x00FFFF,
"black" -> 0x000000,
"orange" -> 0xFF8040,
"brown" -> 0x804000)
def main(args: Array[String]) {
for (name <- args) println(
colors.get(name) match {
case Some(code) =>
name + " has code: " + code
case None =>
"Unknown color: " + name
}
)
}
}
For more explanation on the language click here