Scala
-
Upload
gustavo-fernandes -
Category
Documents
-
view
652 -
download
0
description
Transcript of Scala
![Page 1: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/1.jpg)
Gustavo Fernandes
Scala Supersonic Tour
![Page 2: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/3.jpg)
Why care?
• Run on JVM
• Statically typed
• Concise and powerfull
• Seamless integration/drop in replacement for Java
• Grow with you
![Page 4: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/6.jpg)
Features – Conciseness (Cont.)
case class Beer(name: String, price: Int)
SCALA
![Page 7: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/7.jpg)
Features – Actors
• Powerfull abstration for Multithreading programming
• No synchronization blocks and complex libraries
• Simple to use
• Implements Erlang actor model
![Page 8: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/9.jpg)
Features – Collections
• Rich collection library
• Immutable by default
• High performance and concise
![Page 10: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/11.jpg)
Features – Collections (Cont.)
Filtering
val pairs = list.filter(x => x%2==0)
val pairs = list.filter(_%2==0)
![Page 12: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/16.jpg)
There's more...
• Easy to create Domain Specific Languages (DSL)
• Pattern matching
• Annotations
• Generics
![Page 17: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/17.jpg)
Tooling• Eclipse Plugin• Netbeans Plugin (Better)
![Page 18: Scala](https://reader036.fdocuments.in/reader036/viewer/2022081804/54b79f0d4a795997768b45c6/html5/thumbnails/18.jpg)
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>