introduction to scala for java developers

58
Scala for Java Developers Michael Galpin, http://fupeg.blogspot.com Tuesday, January 13, 2009

Upload: michael-galpin

Post on 29-Nov-2014

15.917 views

Category:

Technology


2 download

DESCRIPTION

This is brief presentation on the Scala programming language. It is aimed at Java developers who are curious about Scala. It was given at a San Francisco Java User Group in January 2009.

TRANSCRIPT

Page 1: Introduction to Scala for Java Developers

Scala for Java DevelopersMichael Galpin, http://fupeg.blogspot.com

Tuesday, January 13, 2009

Page 2: Introduction to Scala for Java Developers

Scala is...

✤ A general purpose programming language

✤ A language that runs on the Java VM

✤ Statically typed

✤ An object-oriented language

✤ No primitives or operators, but with singletons

✤ A functional language

✤ A scalable language

Tuesday, January 13, 2009

Page 3: Introduction to Scala for Java Developers

Scala is General Purpose

✤ Some uses of Scala

✤ Scripting

✤ Web applications

✤ Messaging

✤ Graphical User Interfaces

Tuesday, January 13, 2009

Page 4: Introduction to Scala for Java Developers

Scala runs on the JVM

✤ Compiles to Java bytecode

✤ Written by Martin Odersky (javac)

✤ Uses core Java heavily

✤ Can call any Java classes

✤ Can be called by any Java class

Tuesday, January 13, 2009

Page 5: Introduction to Scala for Java Developers

Scala is Statically Typed

var name :String = "Hello, world" ;

Tuesday, January 13, 2009

Page 6: Introduction to Scala for Java Developers

Scala is Statically Typed

var name = "Hello, world" ;

Tuesday, January 13, 2009

Page 7: Introduction to Scala for Java Developers

Scala is Statically Typed

var name = "Hello, world"

Tuesday, January 13, 2009

Page 8: Introduction to Scala for Java Developers

Scala is Statically Typed

name = "Hello, world"val

Tuesday, January 13, 2009

Page 9: Introduction to Scala for Java Developers

Scala is Statically Typed

name = "Hello, world"val

def makeList(a:String, b:String) {

return a :: b :: Nil ;

}

:List[String] =

Tuesday, January 13, 2009

Page 10: Introduction to Scala for Java Developers

Scala is Statically Typed

name = "Hello, world"val

def makeList(a:String, b:String) {

return a :: b :: Nil

}

:List[String] =

Tuesday, January 13, 2009

Page 11: Introduction to Scala for Java Developers

Scala is Statically Typed

name = "Hello, world"val

def makeList(a:String, b:String) {

a :: b :: Nil

}

:List[String] =

Tuesday, January 13, 2009

Page 12: Introduction to Scala for Java Developers

Scala is Statically Typed

name = "Hello, world"val

def makeList(a:String, b:String) a :: b :: Nil:List[String] =

Tuesday, January 13, 2009

Page 13: Introduction to Scala for Java Developers

Scala is Statically Typed

name = "Hello, world"val

def makeList(a:String, b:String) a :: b :: Nil =

Tuesday, January 13, 2009

Page 14: Introduction to Scala for Java Developers

Scala is object-oriented

✤ Classes

class Computer (val clockSpeed:Double, var memory:Int, var hdd:Int, var os:String){ def this(clockSpeed:Double) = this(clockSpeed, 0, 0, null) def addMemory(newRam:Int):Unit = { this.memory += newRam } def addDrive(newHdd:Int) = { this.hdd += newHdd } override def toString = clockSpeed + " GHz " + memory + " GB RAM " + hdd + " GB hdd " + os }

Tuesday, January 13, 2009

Page 15: Introduction to Scala for Java Developers

Scala is object-oriented

✤ Inheritanceclass Laptop(clockSpeed:Double, memory:Int, hdd:Int, val screenSize:Int, os:String) extends Computer(clockSpeed,memory,hdd,os){ override def toString = screenSize + " inch screen " + super.toString}

✤ Mix-instrait Mac{ var osVersion:String def osx = { osVersion match { case "10.5" => "Leopard" case "10.4" => "Tiger" case _ => "OSX" } }}

class MacBook(clockSpeed:Double, memory:Int, hdd:Int, os:String) extends Laptop(clockSpeed, memory, hdd, 13, os) with Mac{ var osVersion = os}

Tuesday, January 13, 2009

Page 16: Introduction to Scala for Java Developers

Scala has More Effective Java

Tuesday, January 13, 2009

Page 17: Introduction to Scala for Java Developers

Scala has More Effective Java

✤ You always override equals, hashCode, and toString ... right?

Tuesday, January 13, 2009

Page 18: Introduction to Scala for Java Developers

Scala has More Effective Java

✤ You always override equals, hashCode, and toString ... right?

✤ Case Classescase class Player(val name:String, val team:String, val position:String)val tebow = Player("Tim Tebow","Florida","QB")println(tebow) // prints (Time Tebow,Florida,QB)

val clone = Player("Tim Tebow","Florida","QB")println(clone == tebow) // prints true

val set = new HashSet[Player]set += tebowset += clone

println(set.size) // prints 1

Tuesday, January 13, 2009

Page 19: Introduction to Scala for Java Developers

Scala is very object-oriented

✤ No primitives

✤ No int, just Int

✤ No String[], just Array[String]

✤ No operators

✤ + - * / ++ += are all just methods

✤ Create your own or overload (if not final)

Tuesday, January 13, 2009

Page 20: Introduction to Scala for Java Developers

No Operators, Just Methods

Tuesday, January 13, 2009

Page 21: Introduction to Scala for Java Developers

No Operators, Just Methods

✤ Dots and parentheses are optionalval x = 1.+(2)val y = 1 + 2println(x == y) // prints true

Tuesday, January 13, 2009

Page 22: Introduction to Scala for Java Developers

No Operators, Just Methods

✤ Dots and parentheses are optionalval x = 1.+(2)val y = 1 + 2println(x == y) // prints true

✤ Sweet Syntactic Sugarval cache = new HashMap[String, String]cache += ("foo","bar")cache -= "foo"

Tuesday, January 13, 2009

Page 23: Introduction to Scala for Java Developers

Great for DSLs

✤ Domain Specific Languages

✤ XML

✤ Actors

Tuesday, January 13, 2009

Page 24: Introduction to Scala for Java Developers

XML Example: Atom

class Entry(var title:String, val link:String, val id:String, var updated:Date, var summary:String){ def toAtom = <entry> <link href={link}/> <id>{id}</id> <updated>{updated}</updated> <summary>{summary}</summary> </entry>}

Tuesday, January 13, 2009

Page 25: Introduction to Scala for Java Developers

XML Example: Atomclass Feed(val title:String, val link:String, var updated:Date, val author:String, val id:String){ var entries:List[Entry] = Nil def addEntry(entry:Entry){ entries = entry :: entries } def toAtom = <feed> <title>{title}</title> <link href={link}/> <updated>{updated}</updated> <author> <name>{author}</name> </author> <id>{id}</id> { val nodes = new NodeBuffer for (entry <- entries) { nodes &+ entry.toAtom } nodes } </feed> }

Tuesday, January 13, 2009

Page 26: Introduction to Scala for Java Developers

XML Example: Atom

object Atom{ def main(args:Array[String]){ val f = new Feed("Programming and Politics", "http://fupeg.blogspot.com", new Date(),"Michel Galpin","5819005") val e = new Entry("New Scala Article", "http://fupeg.blogspot.com/2008/04/new-scala-article.html", "6009113042595594848", new Date(), "Article on Scala and XML" ) f.addEntry(e) println(f.toAtom) }}

Tuesday, January 13, 2009

Page 27: Introduction to Scala for Java Developers

XML Example: Atom

$ scalac Atom.scala$ scala Atom<feed> <title>Programming and Politics</title> <link href="http://fupeg.blogspot.com"></link> <updated>Tue Jan 13 11:35:51 PST 2009</updated> <author> <name>Michel Galpin</name> </author> <id>5819005</id> <entry> <link href="http://fupeg.blogspot.com/2008/04/new-scala-article.html"></link> <id>6009113042595594848</id> <updated>Tue Jan 13 11:35:51 PST 2009</updated> <summary>Scala is good</summary> </entry> </feed>

Tuesday, January 13, 2009

Page 28: Introduction to Scala for Java Developers

Singletons in Scala

✤ Use object instead of classobject ComputerStore{ def main(args:Array[String]) = { val newton = new MacBook(2.0, 2048, 120, "10.5") println(newton.clockSpeed + " is fast!") println("newton is running " + newton.osx) }}

✤ Often used as factories a.k.a. companion objectsval names = List("Bill", "David", "Michael")

Tuesday, January 13, 2009

Page 29: Introduction to Scala for Java Developers

Scala is functional

✤ A function is an object, its definition is its apply methodobject DotProdct{ def apply(list1:List[Number], list2:[Number]) = { var result = 0 for (i <- 0 until list1.size){ result += list1(i)*list2(i) } result }}val a = List(1,2,3)val b = List(4,5,6)println(DotProduct(a,b)) // prints 32

Tuesday, January 13, 2009

Page 30: Introduction to Scala for Java Developers

Closures

✤ Functions can be passed as parameters to other functions

✤ Scala allows for inline (anonymous) functions

✤ Still statically typed

✤ Lexically scoped (anonymous or not)

Tuesday, January 13, 2009

Page 31: Introduction to Scala for Java Developers

Closure Examples

Tuesday, January 13, 2009

Page 32: Introduction to Scala for Java Developers

Closure Examples

class Person(val firstName:String, val middleName:String, var lastName:String){ def print( formatter:(String,String,String) => String )={ val str = formatter(firstName,middleName,lastName) println(str) }}

Tuesday, January 13, 2009

Page 33: Introduction to Scala for Java Developers

Closure Examples

class Person(val firstName:String, val middleName:String, var lastName:String){ def print( formatter:(String,String,String) => String )={ val str = formatter(firstName,middleName,lastName) println(str) }}val p = new Person("Michael","David","Galpin")

Tuesday, January 13, 2009

Page 34: Introduction to Scala for Java Developers

Closure Examples

class Person(val firstName:String, val middleName:String, var lastName:String){ def print( formatter:(String,String,String) => String )={ val str = formatter(firstName,middleName,lastName) println(str) }}val p = new Person("Michael","David","Galpin")

val sep = " "def std(a:String,b:String,c:String) = List(a,b,c).mkString(sep)p.print(std)

Tuesday, January 13, 2009

Page 35: Introduction to Scala for Java Developers

Closure Examples

class Person(val firstName:String, val middleName:String, var lastName:String){ def print( formatter:(String,String,String) => String )={ val str = formatter(firstName,middleName,lastName) println(str) }}val p = new Person("Michael","David","Galpin")

val sep = " "def std(a:String,b:String,c:String) = List(a,b,c).mkString(sep)p.print(std)

val sep2 = ":"p.print( (a,b,c) => a + sep + b + sep2 + c)

Tuesday, January 13, 2009

Page 36: Introduction to Scala for Java Developers

Closure Examples

class Person(val firstName:String, val middleName:String, var lastName:String){ def print( formatter:(String,String,String) => String )={ val str = formatter(firstName,middleName,lastName) println(str) }}val p = new Person("Michael","David","Galpin")

val sep = " "def std(a:String,b:String,c:String) = List(a,b,c).mkString(sep)p.print(std)

val sep2 = ":"p.print( (a,b,c) => a + sep + b + sep2 + c)

p.print(_ + sep + _ + sep2 + _)

Tuesday, January 13, 2009

Page 37: Introduction to Scala for Java Developers

Closures: Not Just for Golf

Tuesday, January 13, 2009

Page 38: Introduction to Scala for Java Developers

Closures: Not Just for Golf

var nums = 1 until 100 filter(_ % 3 == 0)

Tuesday, January 13, 2009

Page 39: Introduction to Scala for Java Developers

Closures: Not Just for Golf

var nums = 1 until 100 filter(_ % 3 == 0)

nums.foreach(println(_))

Tuesday, January 13, 2009

Page 40: Introduction to Scala for Java Developers

Closures: Not Just for Golf

var nums = 1 until 100 filter(_ % 3 == 0)

nums.foreach(println(_))

nums = nums.map(2*_ + 3)println("All greater than 10? " + nums.forall(_ > 10))

Tuesday, January 13, 2009

Page 41: Introduction to Scala for Java Developers

Closures: Not Just for Golf

var nums = 1 until 100 filter(_ % 3 == 0)

nums.foreach(println(_))

nums = nums.map(2*_ + 3)println("All greater than 10? " + nums.forall(_ > 10))

val sum = nums.foldLeft(0)(_+_)println("sum="+sum)

Tuesday, January 13, 2009

Page 42: Introduction to Scala for Java Developers

Closures: Not Just for Golf

var nums = 1 until 100 filter(_ % 3 == 0)

nums.foreach(println(_))

nums = nums.map(2*_ + 3)println("All greater than 10? " + nums.forall(_ > 10))

val sum = nums.foldLeft(0)(_+_)println("sum="+sum)

val sorted = nums.toList.sort( (n,m) => { val remN = n % 5 val remM = m % 5 remN > remM} )sorted.foreach( (i) => println(i + " " + i % 5))

Tuesday, January 13, 2009

Page 43: Introduction to Scala for Java Developers

Scala: A Scalable Language

✤ Compiles to bytecode

✤ Very close to “native” Java

✤ Sometimes faster!

✤ Tail recursion

✤ Concurrency

Tuesday, January 13, 2009

Page 44: Introduction to Scala for Java Developers

Scala: Better Bytecode

What is the first triangle number to have at least N divisors?

Tim

e

N

Tuesday, January 13, 2009

Page 45: Introduction to Scala for Java Developers

Tail Recursion: Scala Factorial

object Factorial{ val ZERO = BigInt.int2bigInt(0) val ONE = BigInt.int2bigInt(1) def factorial(n:BigInt):BigInt= n match { case ZERO => ONE case ONE => ONE case _ => n*factorial(n-1) } def main(args:Array[String]){ val i = args(0).toInt val n = BigInt.int2bigInt(i) val start = new java.util.Date println(factorial(n)) val duration = (new java.util.Date()).getTime - start.getTime println("duration="+duration) }}

Tuesday, January 13, 2009

Page 46: Introduction to Scala for Java Developers

And in Java...

import java.math.BigInteger;import java.util.Date;

public class Factorial{ public static BigInteger factorial(BigInteger n){ if (n.equals(BigInteger.ZERO)) return BigInteger.ONE; else if (n.equals(BigInteger.ONE)) return BigInteger.ONE; else return n.multiply(factorial(n.subtract(BigInteger.ONE))); } public static void main(String[] args){ BigInteger n = new BigInteger(args[0]); Date start = new Date(); System.out.println(factorial(n)); long duration = (new Date()).getTime() - start.getTime(); System.out.println("duration="+duration); }}

Tuesday, January 13, 2009

Page 47: Introduction to Scala for Java Developers

And the Results...

0

25000

50000

75000

100000

10000 20000 30000 50000

Factorial

Time(ms)

N

Java Scala

Tuesday, January 13, 2009

Page 48: Introduction to Scala for Java Developers

Scala Concurrency: Actors

Tuesday, January 13, 2009

Page 49: Introduction to Scala for Java Developers

Scala Concurrency: Actors

✤ Actor Model: A Different Way to Implement Concurrency

Tuesday, January 13, 2009

Page 50: Introduction to Scala for Java Developers

Scala Concurrency: Actors

✤ Actor Model: A Different Way to Implement Concurrency

✤ Each Object is an Actor

Tuesday, January 13, 2009

Page 51: Introduction to Scala for Java Developers

Scala Concurrency: Actors

✤ Actor Model: A Different Way to Implement Concurrency

✤ Each Object is an Actor

✤ Each Actor has a Mailbox

Tuesday, January 13, 2009

Page 52: Introduction to Scala for Java Developers

Scala Concurrency: Actors

✤ Actor Model: A Different Way to Implement Concurrency

✤ Each Object is an Actor

✤ Each Actor has a Mailbox

✤ Actors (Asynchronously) Send Messages and Receive them in their Mailbox

Tuesday, January 13, 2009

Page 53: Introduction to Scala for Java Developers

Scala Concurrency: Actors

✤ Actor Model: A Different Way to Implement Concurrency

✤ Each Object is an Actor

✤ Each Actor has a Mailbox

✤ Actors (Asynchronously) Send Messages and Receive them in their Mailbox

✤ No Shared State

Tuesday, January 13, 2009

Page 54: Introduction to Scala for Java Developers

Why Actors: PerformanceTh

roug

hput

(KB/

s)

Concurrent Sessions

Apache (C/Threads) vs. Yaws (Erlang/Actors)

Tuesday, January 13, 2009

Page 55: Introduction to Scala for Java Developers

Why Actors: Simpler Code

✤ No Code Changes for Multi-threaded vs. Single-threaded

✤ No Mutable State

✤ No (Dead) Locking

Tuesday, January 13, 2009

Page 56: Introduction to Scala for Java Developers

Actor Example

class Pong extends Actor { def act() { var pongCount = 0 loop { react { case Ping => if (pongCount % 1000 == 0) // print every 1000th Ping println("Pong: ping "+pongCount) sender ! Pong pongCount = pongCount + 1 case Stop => println("Pong: stop") exit() } } }}

case object Pingcase object Pongcase object Stop

Tuesday, January 13, 2009

Page 57: Introduction to Scala for Java Developers

Actor Example

class Ping(count: Int, pong: Actor) extends Actor { def act(){ var pingsLeft = count - 1 pong ! Ping loop{ react { case Pong => if (pingsLeft % 1000 == 0) // print every 1000th Pong println("Ping: pong") if (pingsLeft > 0){ pong ! Ping pingsLeft -= 1 } else { println("Ping: stop") pong ! Stop exit() } } } }}

Tuesday, January 13, 2009

Page 58: Introduction to Scala for Java Developers

Actor Example

object PingPong { def main(args:Array[String]){ val pong = new Pong val ping = new Ping(100000, pong) ping.start pong.start }}

Tuesday, January 13, 2009