Scala

889 views

Published on

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

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
889
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
25
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Scala

  1. 1. Scala Supersonic Tour Gustavo Fernandes
  2. 2. 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
  3. 3. Why care? • Run on JVM • Statically typed • Concise and powerfull • Seamless integration/drop in replacement for Java • Grow with you
  4. 4. 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>
  5. 5. Features – Conciseness public class Beer { public String name; public int price; public Beer(String name, int price) { this.name = name; this.price = price; } @Override public String toString() { return "Beer(" + name + "," + price + ")"; } @Override JAVA public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public 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; } }
  6. 6. Features – Conciseness (Cont.) SCALA case class Beer(name: String, price: Int)
  7. 7. Features – Actors • Powerfull abstration for Multithreading programming • No synchronization blocks and complex libraries • Simple to use • Implements Erlang actor model
  8. 8. Features – Actors (cont.) Declaring class BusyActor extends  Actor {     def  doSomeLengthyCalculation() = print("Look ma! No syncronization!")      def act() {         react {             case _ => doSomeLengthyCalculation()         }     } } Using val actor = new BusyActor() actor.start actor !? "A message"           // Send a synchronous message actor !? (300, "My message")   // Send a synchronous message with timeout  actor ! “A message”            // Asynchronous
  9. 9. Features – Collections • Rich collection library • Immutable by default • High performance and concise
  10. 10. Features – Collections (Cont.) Creating val map = Map("key1"­>”value1”, "key2"­>”value2”) val seq = 1 to 10 val list = List(1,2,3,4)
  11. 11. Features – Collections (Cont.) Filtering val pairs = list.filter(x => x%2==0)   val pairs = list.filter(_%2==0)
  12. 12. Features – Collections (Cont.) Looping var list = List(1,2,3,4,5) list.foreach( x => print(x+" ") ) 12345
  13. 13. Features – Collections (Cont.) Reducing val sum = list.foldLeft(0) ( (x:Int,y:Int) => x+y ) val sum = list.foldLeft(0) (_+_) Creating, Filtering and Reducing val res = (0/:(1 to 10).filter(_%2==0))(_+_)
  14. 14. Features - Traits • Interfaces with fields and methods implementation • Add behaviour to a class without messing with the hierarchy • Can be applied in runtime
  15. 15. Features – Traits (Cont.) trait ReflectionStats { def getNumberOfMethods() = this.getClass.getMethods.length } val myList = new java.util.ArrayList with ReflectionStats print( myList.getNumberOfMethods() )
  16. 16. There's more... • Easy to create Domain Specific Languages (DSL) • Pattern matching • Annotations • Generics
  17. 17. Tooling • Eclipse Plugin • Netbeans Plugin (Better)
  18. 18. 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>
  19. 19. Thank you Gustavo Fernandes g.fernandes@sourcesense.com

×