Scala

19
Gustavo Fernandes Scala Supersonic Tour

description

Scala presentation at Sourcesense company meeting held in Amsterdam in October/2009

Transcript of Scala

Page 1: Scala

Gustavo Fernandes

Scala Supersonic Tour

Page 2: Scala

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

Page 3: Scala

Why care?

• Run on JVM

• Statically typed

• Concise and powerfull

• Seamless integration/drop in replacement for Java

• Grow with you

Page 4: Scala

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>

Page 5: Scala

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

Page 6: Scala

Features – Conciseness (Cont.)

case class Beer(name: String, price: Int)

SCALA

Page 7: Scala

Features – Actors

• Powerfull abstration for Multithreading programming

• No synchronization blocks and complex libraries

• Simple to use

• Implements Erlang actor model

Page 8: Scala

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

Page 9: Scala

Features – Collections

• Rich collection library

• Immutable by default

• High performance and concise

Page 10: Scala

Features – Collections (Cont.)

val map = Map("key1"­>”value1”, "key2"­>”value2”)

val seq = 1 to 10

val list = List(1,2,3,4)

Creating

Page 11: Scala

Features – Collections (Cont.)

Filtering

val pairs = list.filter(x => x%2==0)  

val pairs = list.filter(_%2==0)

Page 12: Scala

Features – Collections (Cont.)

Looping

var list = List(1,2,3,4,5)

list.foreach( x => print(x+" ") )

1 2 3 4 5

Page 13: Scala

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

Page 14: Scala

Features - Traits• Interfaces with fields and methods

implementation

• Add behaviour to a class without messing with the hierarchy

• Can be applied in runtime

Page 15: Scala

Features – Traits (Cont.)

trait ReflectionStats { def getNumberOfMethods() = this.getClass.getMethods.length }

val myList = new java.util.ArrayList with ReflectionStats

print( myList.getNumberOfMethods() )

Page 16: Scala

There's more...

• Easy to create Domain Specific Languages (DSL)

• Pattern matching

• Annotations

• Generics

Page 17: Scala

Tooling• Eclipse Plugin• Netbeans Plugin (Better)

Page 18: Scala

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>

Page 19: Scala

Gustavo [email protected]

Thank you