Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float...

65
Scala

Transcript of Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float...

Page 1: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Scala

Page 2: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Finally...

Page 3: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

...something useful to do with the JVM.

Page 4: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Image source: http://www.tripadvisor.com/LocationPhotos-g187789-Lazio.html

Page 5: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

YoungDeveloped in 2003 by Martin Odersky at EPFL

Page 6: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Martin also brought you javac and Java

Generics

Page 7: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Don’t hold that against him, though

Page 8: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

OO/Functional hybrid

Page 9: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Statically typed

Page 10: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Has a REPL (yay!)

Page 11: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Runs on the JVM and the CLR

Page 12: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Image source: http://shootout.alioth.debian.org/gp4/scala.php

Scala vs Java

Page 13: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Image source: http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=scala&lang2=csharp

Scala vs Mono

Page 14: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Can call existing Java code

Page 15: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Java can even call into Scala, too*

(*) most of the time

Page 16: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Same integer and float rules as Java

Page 17: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> 3 / 4res3: Int = 0

scala>

Page 18: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> 3.0 / 4res4: Double = 0.75

scala>

Page 19: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

But, isn’t it statically typed?

Page 20: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

type inference == awesome

Page 21: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> val msg = "Hello, Philly Lambda!"msg: java.lang.String = Hello, Philly Lambda!

scala> val msg2 : String = "Its me again" msg2: String = Its me again

scala>

Page 22: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

c: Int = 10

The colon means “is type of”

Page 23: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Has both values and variables

Page 24: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

values are immutable

Page 25: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> val c = 10000c: Int = 10000

scala> c = 10001line1$object.$iw.$iw.c = 10001<console>:5: error: assignment to non-variable val res0 = {c = 10001;c} ^

scala>

Page 26: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

variables are mutable

Page 27: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> var c = 10000c: Int = 10000

scala> c = 10001c: Int = 10001

scala> cres2: Int = 10001

scala>

Page 28: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> println("Hello, world!")Hello, world!unnamed2: Unit = ()

Page 29: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Unit == void

Page 30: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Methods

Page 31: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> def max(x: Int, y: Int) = if (x > y) x else ymax: (Int,Int)Int

scala> max(3, 5)res5: Int = 5

scala>

Page 32: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Compiler cannot infer method parameter

types

Page 33: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Sometimes it can infer the return parameter

type*

(*) not if its recursive, though

Page 34: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Syntax of anonymous functions in Scala

Image source: http://www.artima.com/scalazine/articles/steps.html

Page 35: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

You can write scripts in Scala, too

Page 36: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

#!/bin/shexec scala $0 $@!#

var i = 0while (i < args.length) { if (i != 0) { print(“ “) } print(args(i)) i += 1}println()

Page 37: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

args.foreach(arg => println(arg))

for (arg <- args) println(arg)

Page 38: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

val ary = new Array[String](3)

val ary: Array[String] = new Array[String](3)

Page 39: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Has both List and Tuple built-ins a la

Python

Page 40: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> val l1 = List(1,2)l1: List[Int] = List(1, 2)

scala> val l2 = List(3,4)l2: List[Int] = List(3, 4)

scala> l1 ::: l2res0: List[Int] = List(1, 2, 3, 4)

scala>

Page 41: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> val x = List[String](1, 2, 3, "hello")<console>:4: error: type mismatch; found : Int(1) required: Stringval x = List[String](1, 2, 3, "hello") ^<console>:4: error: type mismatch; found : Int(2) required: Stringval x = List[String](1, 2, 3, "hello") ^<console>:4: error: type mismatch; found : Int(3) required: Stringval x = List[String](1, 2, 3, "hello") ^

scala>

Page 42: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> (1,2,3)res14: (Int, Int, Int) = (1,2,3)

scala> ("hello",40,List('x','y'))res15: (java.lang.String, Int, List[Char]) = (hello,40,List(x, y))

scala> (1,2,3)._2res16: Int = 2

scala>

Page 43: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Also has Set and Map classes

Page 44: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> val tvShows = Map( | 1 -> "World's Worst Cooking", | 2 -> "American Pariah", | 3 -> "Scala for n00bs", | 4 -> "Guy Steele is Looking for You", | 5 -> "699 Club" | )tvShows: scala.collection.immutable.Map[Int,java.lang.String] = Map(2 -> American Pariah, 4 -> Guy Steele is Looking for You, 1 -> World's Worst Cooking, 3 -> Scala for n00bs, 5 -> 699 Club)

scala>

Page 45: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Classes

Page 46: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

class ConstructorShowoff(message: String) { var junk = “Can I haz Scala, plz?”

def report() = println(message)

def complete() = junk += “ Kthxbye”}

val x = new ConstructorShowoff(“Yo, Adrienne”)x.reportx.completeprintln(x.junk)

Page 47: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

class MoreHotness(message: String) {

if (message == null) throw new NullPointerException(“message was null”)

}

class TwoConstructors(message: String, count: Int) {

def this(message: String) = this(message, 1)

def say() = { for (i <- 1 to count) println(message) }}

Page 48: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

No static fields or methods in Scala classes

Page 49: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Huh?

Page 50: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Instead, Scala has singleton objects

Page 51: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

class ObjectWithCompanion(message: String) { def say() = { val whatToSay = ObjectWithCompanion.prepend(message) println(whatToSay) }}

object ObjectWithCompanion { def prepend(x: String) = “Philly Lambda, “ + x}

Page 52: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

object PLApp { def main(args: Array[String]) { val o = ObjectWithCompanion(“Welcome to Scala!”) o.say() }}

Page 53: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Traits and Mixins

Page 54: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

trait Talky { def greet() = “Hi”}

class WalMartGreeter extends Talky { override def greet() = “Welcome to Wal-Mart!”}

class NewYorker extends Talky { override def greet() = “Fuck you”}

var mouth: Talky = new NewYorkerprintln(mouth.greet())

Page 55: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

trait Unsure extends Talky { override def greet() = super.greet() + “?”}

val mouth: Talky = new NewYorker with Unsureprintln(mouth.greet())

Page 56: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Something that will make Ed happy

Page 57: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

def approximate(guess: Domain) : Domain = if (isGoodEnough(guess)) guess else approximate(improve(guess))

def approximate(initialGuess: Domain) : Domain = { var guess = initialGuess while (!isGoodEnough(guess)) guess = improve(guess) guess}

Page 58: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

They are the same

Page 59: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Scala supports TCO

Page 60: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Has pattern matching a la Prolog/Erlang, as well

Page 61: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Actors

Page 62: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

scala> import scala.actors.Actor._import scala.actors.Actor._

scala> val myActor = actor { | for (i <- 1 to 5) | println("Anyone awake out there?") | Thread.sleep(1000) | }myActor: scala.actors.Actor = scala.actors.Actor$$anon$0@7ddc70

scala> Anyone awake out there?Anyone awake out there?Anyone awake out there?Anyone awake out there?Anyone awake out there?

scala>

Page 63: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

Uses the ! to send messages and pattern

matching for receive

Page 64: Scala - cbcg.netJava can even call into Scala, too* (*) most of the time. Same integer and float rules as Java. scala> 3 / 4 res3: Int = 0 scala> scala> 3.0 / 4 res4: Double = 0.75

val echoActor = actor { while (true) { receive { case msg => println(“received message: “ + msg) } }}

echoActor ! “Uh, time to wrap it up, d00d”