Gustavo Fernandes
Scala Supersonic Tour
What is Scala?
• A JVM programming language
• Open Source (BSD-like license)
• Functional + OO multiparadigm
• Created by Martin Odersky at University of Lausanne in 2003
Why care?
• Run on JVM
• Statically typed
• Concise and powerfull
• Seamless integration/drop in replacement for Java
• Grow with you
Features – Native XML• Declaring
var axml = <root><child1>value</child1></root>
• Reading
var childValue = axml \\ "child1".text
• Reading from file
var desc = scala.xml.XML.loadFile("web.xml") \\ "description" text
• Producing
var currentMillis = <now>{System.currentTimeMillis}</now>
Features – Concisenesspublic class Beer {
public String name;public int price;
public Beer(String name, int price) {this.name = name;this.price = price;
}
@Overridepublic String toString() {
return "Beer(" + name + "," + price + ")";}
@Overridepublic int hashCode() {
final int prime = 31;int result = 1;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;
}
@Overridepublic boolean equals(Object obj) {
if (this == obj)return true;
if (obj == null)return false;
if (getClass() != obj.getClass())return false;
Beer other = (Beer) obj;if (name == null) {
if (other.name != null)return false;
} else if (!name.equals(other.name))return false;
return true;}
}
JAVA
Features – Conciseness (Cont.)
case class Beer(name: String, price: Int)
SCALA
Features – Actors
• Powerfull abstration for Multithreading programming
• No synchronization blocks and complex libraries
• Simple to use
• Implements Erlang actor model
Features – Actors (cont.)
class BusyActor extends Actor {
def doSomeLengthyCalculation() = print("Look ma! No syncronization!")
def act() { react { case _ => doSomeLengthyCalculation() } }}
val actor = new BusyActor()actor.start
actor !? "A message" // Send a synchronous messageactor !? (300, "My message") // Send a synchronous message with timeout actor ! “A message” // Asynchronous
Declaring
Using
Features – Collections
• Rich collection library
• Immutable by default
• High performance and concise
Features – Collections (Cont.)
val map = Map("key1">”value1”, "key2">”value2”)
val seq = 1 to 10
val list = List(1,2,3,4)
Creating
Features – Collections (Cont.)
Filtering
val pairs = list.filter(x => x%2==0)
val pairs = list.filter(_%2==0)
Features – Collections (Cont.)
Looping
var list = List(1,2,3,4,5)
list.foreach( x => print(x+" ") )
1 2 3 4 5
Features – Collections (Cont.)
Reducing
val sum = list.foldLeft(0) ( (x:Int,y:Int) => x+y )
val sum = list.foldLeft(0) (_+_)
val res = (0/:(1 to 10).filter(_%2==0))(_+_)
Reducing
Creating, Filtering and Reducing
Features - Traits• Interfaces with fields and methods
implementation
• Add behaviour to a class without messing with the hierarchy
• Can be applied in runtime
Features – Traits (Cont.)
trait ReflectionStats { def getNumberOfMethods() = this.getClass.getMethods.length }
val myList = new java.util.ArrayList with ReflectionStats
print( myList.getNumberOfMethods() )
There's more...
• Easy to create Domain Specific Languages (DSL)
• Pattern matching
• Annotations
• Generics
Tooling• Eclipse Plugin• Netbeans Plugin (Better)
Tooling - Maven
Project Archetype
mvn archetype:create -DarchetypeGroupId=org.scala-tools.archetypes -DarchetypeArtifactId=scala-archetype-simple -DarchetypeVersion=1.2 -DremoteRepositories=http://scala-tools.org/repo-releases -DgroupId=com.mycompany.scala -DartifactId=scalaTest
<plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <id>compile</id> <goals> <goal>compile</goal> </goals> <phase>compile</phase> </execution </executions></plugin>
Top Related