scala @ techmeetup edinburgh
DESCRIPTION
TechMeetup Edinburgh introductory talk on Scala with a taste of some of its features.TRANSCRIPT
Scala@ TechMeetup Edinburgh
Stuart [email protected]
What does ProInnovate do?
TOP SECRET
The basics…
• Scala stands for ‘scalable language’• Scala runs on the Java Virtual Machine ( JVM)• Created by Martin Odersky (EPFL)
• he previously created the Pizza language with Philip Wadler (now at Edinburgh University)
• …then GJ (Generic Java) that became Java Generics in the official Java 5.0 release for which he apologises!
How long has it been around?
• Scala 1.0 appeared in 2003• Scala 2.0 appeared in 2006• Scala 2.7.7 is the current stable release
• Scala 2.8 beta 1 is due for release any day now!
“If I were to pick a language to use today other than Java, it would be Scala”
James Gosling
Try this at home!
• Scala home: http://www.scala-lang.org/• Downloadable for Mac, Linux & Windows• Shell interpreter: scala• Compilers: scalac and fsc• Documentation generator scaladoc• Plugins for Eclipse, Netbeans, IntelliJ
Hello World!->scalaWelcome to Scala version 2.7.7.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).Type in expressions to have them evaluated.Type :help for more information.
scala> def main { println("Hello World!") }main: Unit
scala> mainHello World!
Build tools +
• Maven Plugin (no endorsement implied)—http://scala-tools.org/mvnsites/maven-scala-plugin/
• simple-build-tool—http://code.google.com/p/simple-build-tool/
• Apache Ant tasks for Scala—http://www.scala-lang.org/node/98
• Apache Buildr—http://buildr.apache.org/• JavaRebel—http://www.zeroturnaround.com/
How does Scala differfrom Java?
• Object oriented and functional
• Everything is an object• First-class functions
(‘closures’)• Singleton objects• Mixin composition with
Traits
• Pattern matching and Extractors
• Actors• XML literals• Properties• Case classes• Lazy evaluation• Parser combinators• Tuples
Statically Typed
Statically Typed Dynamically Typed
Java, Scala Ruby, Python, Groovy, JavaScript
Dynamic typing in JavaScript
> function calc(x) { return ((x + 2) * 2) }undefined
> calc(1)6
> calc("1")24
Dynamic typing in Groovy
groovy:000> def calc(x) { ((x + 2) * 2) }===> true
groovy:000> calc(1)===> 6
groovy:000> calc("1")===> 1212
Static Typing in Scalascala> def calc(x:Int) = ((x + 2) * 2)calc: (x: Int)Int
scala> calc(1)res0: Int = 6
scala> calc("1")<console>:6: error: type mismatch; found : java.lang.String("1") required: Int calc("1") ^
Static Typing in Scalascala> def calc(x:Any) = x match { | case y:Int => ((y + 2) * 2) | case y:String => ((y + 2) * 2) | }calc: (x: Any)Any
scala> calc(1)res1: Any = 6
scala> calc("1")res2: Any = 1212
Date x = new Date();
val x = new Datex: java.util.Date = Tue Jan 12 01:04:42 GMT 2010
List<Integer> y = new ArrayList<Integer>();Collections.addAll(y,1,2,3);
val y = List(1,2,3)y: List[Int] = List(1, 2, 3)
Type inferenceJava
Scala
Java
Scala
Everything is an object
“Answer = ” + 6 * 4
“Answer = ”.+(6.*(4))
String s = "Which are the longer words";Array<String> words = s.split(‘ ’);ArrayList<String> longWords = new ArrayList<String>();for (w in words) { if (w.size() > 3) longWords.add(w);}System.out.println(longWords);
Concise / first-class functionsJava
val s = "Which are the longer words"val longWords = s.split(‘ ’).filter( _.size > 3)println(longWords)
Scala
scala> val words = List("three","words","first")words: List[java.lang.String] = List(two, words, first)
scala> val List(x, y, z) = words.sort( (a,b) => a < b )x: java.lang.String = firsty: java.lang.String = threez: java.lang.String = words
scala> val (x :: _) = words.sort( _ < _ )x: java.lang.String = first
Pattern matching
public class Rect { private final int width; private final int height;
public Rect(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return this.width; } public int getHeight() { return this.height; } public int area() { return this.width * this.height; }}…Rect r = new Rect(2,4);System.out.println(r.area());
Properties, getters and settersJava
case class Rect(width:Int, height:Int) { def area = width * height}
val r = Rect(2,4)println(r.area)
Scala
Regular expressions and extractors
val Email = """([A-Za-z0-9\._%-]+)@([A-Za-z0-9\.-]+\.[A-Za-z]{2,4})""".rval Telephone = """\+(\d+) ([\d ]+)""".r
val inputs = List("+44 2423 1313", "[email protected]", "Fred Bloggs")
inputs.foreach{ input => val output = input match { case Email(user, domain) => "Email => User:" + user + " Domain:" + domain case Telephone(country, code) => "Telephone => Country:" + country + " Code:" + code case _ => "Unknown" } println(output)}
Telephone => Country:44 Code:2423 1313Email => User:test Domain:gmail.co.ukUnknown
XML literals and parsing
val xml = <item> <title>Lift Version 1.0 Released</title> <link>http://www.scala-lang.org/node/1011</link> <description>Twelve is > six</description> <comments>http://www.scala-lang.org/node/1011#comments</comments> <category domain="http://www.scala-lang.org/taxonomy/term/15">Featured</category> <pubDate>Fri, 27 Feb 2009 10:02:55 +0000</pubDate> <dc:creator>bagwell</dc:creator> <guid isPermaLink="false">1011 at http://www.scala-lang.org</guid> </item>xml: scala.xml.Elem = …(xml \\ "description").text res1: String = Twelve is > six(xml \\ "category" \ "@domain" ).textres2: String = http://www.scala-lang.org/taxonomy/term/15
Further XML file parsing +
import xml.XML
val loadnode = XML.loadFile(“input.xml”)
println(“Number of items = ” + (loadnode \\ “item”).toList.size)
Number of items = 2
val authors = (loadnode \\ “item” \ “creator”).toList.map(_.text ).toSet.toList.sort(_<_)
println(“Authors were: ” + authors.mkString(“, ”))Authors were: admin, bagwell
Commercial users of Scala
• Twitter—Scala back end Ruby front end• LinkedIn• Foursquare—Scala and Lift• Siemens—Scala and Lift• SAP• EDF• Sony Pictures (ImageWorks)• Nature Magazine• …and Google
Questions?