scala @ techmeetup edinburgh

28
Scala @ TechMeetup Edinburgh Stuart Roebuck [email protected]

Upload: sroebuck

Post on 28-Jan-2015

118 views

Category:

Technology


0 download

DESCRIPTION

TechMeetup Edinburgh introductory talk on Scala with a taste of some of its features.

TRANSCRIPT

Page 1: Scala @ TechMeetup Edinburgh

Scala@ TechMeetup Edinburgh

Stuart [email protected]

Page 2: Scala @ TechMeetup Edinburgh

What does ProInnovate do?

TOP SECRET

Page 3: Scala @ TechMeetup Edinburgh

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!

Page 4: Scala @ TechMeetup Edinburgh

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!

Page 5: Scala @ TechMeetup Edinburgh

“If I were to pick a language to use today other than Java, it would be Scala”

James Gosling

Page 6: Scala @ TechMeetup Edinburgh

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

Page 7: Scala @ TechMeetup Edinburgh

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!

Page 8: Scala @ TechMeetup Edinburgh

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/

Page 9: Scala @ TechMeetup Edinburgh

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

Page 10: Scala @ TechMeetup Edinburgh

Statically Typed

Statically Typed Dynamically Typed

Java, Scala Ruby, Python, Groovy, JavaScript

Page 11: Scala @ TechMeetup Edinburgh

Dynamic typing in JavaScript

> function calc(x) { return ((x + 2) * 2) }undefined

> calc(1)6

> calc("1")24

Page 12: Scala @ TechMeetup Edinburgh

Dynamic typing in Groovy

groovy:000> def calc(x) { ((x + 2) * 2) }===> true

groovy:000> calc(1)===> 6

groovy:000> calc("1")===> 1212

Page 13: Scala @ TechMeetup Edinburgh

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") ^

Page 14: Scala @ TechMeetup Edinburgh

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

Page 15: Scala @ TechMeetup Edinburgh

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

Page 16: Scala @ TechMeetup Edinburgh

Everything is an object

“Answer = ” + 6 * 4

“Answer = ”.+(6.*(4))

Page 17: Scala @ TechMeetup Edinburgh

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

Page 18: Scala @ TechMeetup Edinburgh

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

Page 19: Scala @ TechMeetup Edinburgh

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

Page 20: Scala @ TechMeetup Edinburgh

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

Page 21: Scala @ TechMeetup Edinburgh

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 &gt; 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

Page 22: Scala @ TechMeetup Edinburgh

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

Page 23: Scala @ TechMeetup Edinburgh

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

Page 26: Scala @ TechMeetup Edinburgh
Page 27: Scala @ TechMeetup Edinburgh

Questions?

Page 28: Scala @ TechMeetup Edinburgh