Slide 1

Slide 1 text

Introduction to Scala Aleksandar Prokopec EPFL

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Pragmatic Since 2003 runs on the JVM Seamless Java interoperability Statically typed Production ready Martin Odersky Hybrid

Slide 4

Slide 4 text

Statically typed

Slide 5

Slide 5 text

runs on the JVM

Slide 6

Slide 6 text

Scala programs are fast.

Slide 7

Slide 7 text

OOP + FP

Slide 8

Slide 8 text

“I can honestly say if someone had shown me the Programming Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy.“ James Strachan, creator of Groovy

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Pragmatic

Slide 11

Slide 11 text

Scala is lightweight.

Slide 12

Slide 12 text

println(“Hello world!”)

Slide 13

Slide 13 text

scala> println(“Hello world!”) Hello world! REPL evaluating expressions on the fly

Slide 14

Slide 14 text

object MyApp extends App { println(“Hello world!”) } Compiled version

Slide 15

Slide 15 text

object MyApp extends App { println(“Hello world!”) } Singleton objects no more static methods

Slide 16

Slide 16 text

object MyApp { def main(args: Array[String]) { println(“Hello world!”) } } Declaring methods

Slide 17

Slide 17 text

object MyApp { def main(args: Array[String]) { var user: String = args(0) println(“Hello, ”+user+“!”) } } Declaring variables

Slide 18

Slide 18 text

object MyApp { def main(args: Array[String]) { val user: String = args(0) println(“Hello, ”+user+“!”) } } Declaring values prevents accidental changes

Slide 19

Slide 19 text

object MyApp { def main(args: Array[String]) { val user = args(0) println(“Hello, ”+user+“!”) } } Local type inference less… “typing”

Slide 20

Slide 20 text

class StringArrayFactory { def create: Array[String] = { val array: Array[String] = Array[String](“1”, “2”, “3”) array } } Local type inference less… “typing”

Slide 21

Slide 21 text

class StringArrayFactory { def create = { val array = Array(“1”, “2”, “3”) array } } Local type inference less… “typing”

Slide 22

Slide 22 text

// Scala class Person( var name: String, var age: Int ) Declaring classes …concisely // Java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } }

Slide 23

Slide 23 text

Scala is object-oriented.

Slide 24

Slide 24 text

object Foo { val b = new ArrayBuffer[Any] } Object-oriented everything’s an object

Slide 25

Slide 25 text

Object-oriented everything’s an object Any AnyRef AnyVal String Boolean Char Int

Slide 26

Slide 26 text

object Foo { val b = new ArrayBuffer[Any] b += 1 b += 1.toString b += Foo println(b) } Object-oriented everything’s an object

Slide 27

Slide 27 text

1 + 2 1.+(2) Array(1, 2, 3) ++ Array(4, 5, 6) Array(1, 2, 3).++(Array(4, 5, 6)) 1 :: List(2, 3) List(2, 3).::(1) Operator overloading operators are methods

Slide 28

Slide 28 text

trait Iterator[T] { def next(): T def hasNext: Boolean } Declaring traits traits are like interfaces

Slide 29

Slide 29 text

trait Iterator[T] { def next(): T def hasNext: Boolean def printAll() = while (hasNext) println(next()) } Declaring traits traits are rich

Slide 30

Slide 30 text

trait Animal Multiple inheritance traits are composable

Slide 31

Slide 31 text

trait Animal trait Mammal extends Animal { def think() = println(“hm...”) } Multiple inheritance traits are composable

Slide 32

Slide 32 text

trait Animal trait Mammal extends Animal { def think() = println(“hm...”) } trait Bird extends Animal { def layEgg() = System.createEgg() } Multiple inheritance traits are composable

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

trait Animal trait Mammal extends Animal { def think() = println(“hm...”) } trait Bird extends Animal { def layEgg() = System.createEgg() } class Platypus extends Bird with Mammal Mixin composition traits are composable

Slide 35

Slide 35 text

trait Animal trait Reptile extends Animal { def layInTheSun: Unit = {} } class Dog(name: String) extends Mammal new Dog(“Nera”) with Reptile Dynamic mixin composition …or composition “on the fly”

Slide 36

Slide 36 text

Cake pattern

Slide 37

Slide 37 text

trait Logging { def log(msg: String) } trait AnsweringMachine { self: Logging with DAO with Protocol => log(“Initializing.”) ... } Self-types to express requirements

Slide 38

Slide 38 text

trait ConsoleLogging { def log(msg: String) = println(msg) } class LocalAnsweringMachine extends AnsweringMachine with ConsoleLogging with H2DAO with JabberProtocol Cake pattern layers above layers

Slide 39

Slide 39 text

Scala is functional.

Slide 40

Slide 40 text

(x: Int) => x + 1 First class functions

Slide 41

Slide 41 text

val doub: Int => Int = (x: Int) => x * 2 doub(1)  2 First class functions functions are objects too

Slide 42

Slide 42 text

val doub = (x: Int) => x * 2 List(1, 2, 3).map(doub)  List(2, 4, 6) First class functions as higher order parameters

Slide 43

Slide 43 text

List[Int](1, 2, 3).map((x: Int) => x * 2) // more type inference List(1, 2, 3).map(x => x * 2) // or even shorter List(1, 2, 3).map(_ * 2) Functions with sugar make code sweeter

Slide 44

Slide 44 text

var step = 1 val inc = x => x + step inc(5)  6 step = 2 inc(5)  7 Closures functions that “capture” their environment

Slide 45

Slide 45 text

// Java button.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { System.out.println(e); } } // Scala listenTo(button) reactions += { case e => println(e) } First class functions because you write them in Java all the time

Slide 46

Slide 46 text

Pattern matching

Slide 47

Slide 47 text

Pattern matching …is concise // Scala reactions += { case m: MouseEntered => println(“I see it!”) case m: MouseExited => println(“Lost it.”) case m: MouseClicked => println(“Poked!”) } // Java button.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseEntered(MouseEvent e) { System.out.println(“I see it!”); } public void mouseExited(MouseEvent e) { System.out.println(“Lost it.”); } public void mouseClicked(MouseEvent e) { System.out.println(“Poked!”); } } // ...alternative - isinstanceof

Slide 48

Slide 48 text

trait Tree case class Node(l: Tree, r: Tree) extends Tree case object Leaf extends Tree Pattern matching …is precise

Slide 49

Slide 49 text

def b(t: Tree): Int = t match { case Node(Leaf, Node(_, _)) | Node(Node(_, _), Leaf) => -1 case Node(l, r) => val (ld, rd) = (b(l), b(r)) if (ld == rd) ld + 1 else -1 case Leaf => 0 case _ => error(“Unknown tree!”) } Pattern matching …is precise

Slide 50

Slide 50 text

sealed trait Tree ... def b(t: Tree): Int = t match { case Node(Leaf, Node(_, _)) | Node(Node(_, _), Leaf) => -1 case Node(l, r) => val (ld, rd) = (b(l), b(r)) if (ld == rd) ld + 1 else -1 case Leaf => 0 } Pattern matching …is exhaustive

Slide 51

Slide 51 text

def matchingMeSoftly(a: Any): Any = a match { case 11 => “eleven” case s: String => “’%s’”.format(s) case {t} => t case Array(1, 2, 3) => “1, 2, 3” case head :: tail => tail case _ => null } Pattern matching …is extensible

Slide 52

Slide 52 text

Lazyness

Slide 53

Slide 53 text

lazy values don’t compute if there’s no demand class User(id: Int) { lazy val followernum = from(followers)(f => where(id === f.fid) compute(countDistinct(f.fid)) ) }

Slide 54

Slide 54 text

Call by name evaluate only when you have to def withErrorOut(body: =>Unit) = { val old = Console.out Console.setOut(Console.err) try body finally Console.setOut(old) } ... withErrorOut { if (n < 0) println(“n too small”) }

Slide 55

Slide 55 text

Streams lazy lists e = ∑ 1/n! = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + …

Slide 56

Slide 56 text

Streams lazy lists e = ∑ 1/n! = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + … 0! ?

Slide 57

Slide 57 text

Streams lazy lists e = ∑ 1/n! = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + … 0! 1! 2! 3! ? 0! ?

Slide 58

Slide 58 text

Streams lazy lists e = ∑ 1/n! = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + … 0! 1! 2! 3! ? 0! ? 0! 1/1! 1/2! 1/3! ?

Slide 59

Slide 59 text

Streams lazy lists def fact(n: Int, p: Int): Stream[Int] = p #:: fact(n + 1, p * (n + 1)) val factorials = fact(0, 1) val e = factorials.map(1./_).take(10).sum

Slide 60

Slide 60 text

Scala is expressive.

Slide 61

Slide 61 text

for comprehensions traverse anything for (x <- List(1, 2, 3)) println(x) List(1, 2, 3).foreach(x => println(x)) for (x <- 0 until 10) println(x) (0 until 10).foreach(x => println(x)) Range(0, 10, 1).foreach(x => println(x))

Slide 62

Slide 62 text

for comprehensions map anything for (x <- List(1, 2, 3)) yield x * 2 List(1, 2, 3).map(x => x * 2) for (x <- List(1, 2); y <- List(1, 2)) yield x * y List(1, 2).flatMap(x => List(1, 2).map(y => x * y) )  List(1, 2, 2, 4)

Slide 63

Slide 63 text

for comprehensions like SQL queries for { p <- people if p.age > 25 s <- schools if p.degree == s.degree } yield (p, s) // pairs of people older than 25 and // schools they possibly attended

Slide 64

Slide 64 text

Collections easy to create val phonebook = Map( “Jean” -> “123456”, “Damien” -> “666666”) val meetings = ArrayBuffer( “Dante”, “Damien”, “Sophie”) println(phonebook(meetings(1)))

Slide 65

Slide 65 text

Collections high-level combinators // Java boolean isOk = true for (int i = 0; i < name.length(); i++) { if (isLetterOrDigit(name.charAt(i)) { isOk = false; break; } }

Slide 66

Slide 66 text

Collections high-level combinators // Scala name.forall(_.isLetterOrDigit)

Slide 67

Slide 67 text

Collections high-level combinators // count the total number of different // surnames shared by at least 2 adults people

Slide 68

Slide 68 text

Collections high-level combinators // count the total number of different // surnames shared by at least 2 adults people.filter(_.age >= 18)

Slide 69

Slide 69 text

Collections high-level combinators // count the total number of different // surnames shared by at least 2 adults people.filter(_.age >= 18) .groupBy(_.surname): Map[String, List[Person]]

Slide 70

Slide 70 text

Collections high-level combinators // count the total number of different // surnames shared by at least 2 adults people.filter(_.age >= 18) .groupBy(_.surname): Map[String, List[Person]] .count { case (s, l) => l.size >= 2 }

Slide 71

Slide 71 text

Lists an immutable sequence val countdown = List(3, 2, 1) 3 2 1 countdown

Slide 72

Slide 72 text

Lists an immutable sequence val countdown = List(3, 2, 1) val longer = 4 :: countdown 3 2 1 4 countdown longer

Slide 73

Slide 73 text

Lists an immutable sequence val countdown = List(3, 2, 1) val longer = 4 :: countdown val fast = 10 :: countdown 3 2 1 4 10 countdown longer fast

Slide 74

Slide 74 text

Lists an immutable sequence val countdown = List(3, 2, 1) val longer = 4 :: countdown val fast = 10 :: countdown val withzero = countdown ::: List(0) 3 2 1 4 10 3 2 1 0 countdown longer fast withzero

Slide 75

Slide 75 text

Buffers mutable sequences val b = ArrayBuffer(1, 2, 3) b += 4 b += 5 b += 6  ArrayBuffer(1, 2, 3, 4, 5, 6)

Slide 76

Slide 76 text

Maps mutable or immutable, sorted or unsorted import collection._ val m = mutable.Map(“Heidfeld” -> 1, “Schumacher” -> 2) m += “Hakkinen” -> 3 val im = immutable.Map(“Schumacher” -> 1)

Slide 77

Slide 77 text

Hash tries persistence through efficient structural sharing val im0: Map[Int, Int] = ... im0

Slide 78

Slide 78 text

Hash tries persistence through efficient structural sharing val im0: Map[Int, Int] = ... val im1 = im0 + (1 -> 1) im0 im1

Slide 79

Slide 79 text

Hash tries persistence through efficient structural sharing val im0: Map[Int, Int] = ... val im1 = im0 + (1 -> 1) val im2 = im1 + (2 -> 2) im0 im1 im2

Slide 80

Slide 80 text

Hash tries persistence through efficient structural sharing val im0: Map[Int, Int] = ... val im1 = im0 + (1 -> 1) val im2 = im1 + (2 -> 2) val im3 = im2 + (3 -> 6) im0 im1 im2 im3

Slide 81

Slide 81 text

Hash tries persistence through efficient structural sharing im0 im1 im2 im3 2x-3x slower lookup 2x faster iteration

Slide 82

Slide 82 text

Parallel collections parallelize bulk operations on collections def cntEqlen(m: Map[String, String]) = { m.par.count { case (n, s) => n.length == s.length } } // be careful with side-effects

Slide 83

Slide 83 text

Scala is extensible.

Slide 84

Slide 84 text

One ring to rule them all.

Slide 85

Slide 85 text

actors road to safer concurrency val a = actor { react { case i: Int => println(i) } } ... a ! 5

Slide 86

Slide 86 text

Custom control flow it’s all about control def myWhile(c: =>Boolean)(b: =>Unit) { if (c) { b myWhile(c)(b) } }

Slide 87

Slide 87 text

Custom control flow it’s all about control @tailrec def myWhile(c: =>Boolean)(b: =>Unit) { if (c) { b myWhile(c)(b) } }

Slide 88

Slide 88 text

Custom control flow it’s all about control @tailrec def myWhile(c: =>Boolean)(b: =>Unit) { if (c) { b myWhile(c)(b) } } var i = 0 myWhile (i < 5) { i += 1 }

Slide 89

Slide 89 text

ARM automatic resource management withFile (“~/.bashrc”) { f => for (l <- f.lines) { if (“#”.r.findFirstIn(l) != None) println(l) } }

Slide 90

Slide 90 text

ScalaTest behavioral testing framework “A list” should { “be a double reverse of itself” in { val ls = List(1, 2, 3, 4, 5, 6) ls.reverse.reverse should equal (ls) } }

Slide 91

Slide 91 text

BaySick Basic DSL in Scala 10 PRINT “Baysick Lunar Lander v0.9” 20 LET ('dist := 100) 30 LET ('v := 1) 40 LET ('fuel := 1000) 50 LET ('mass := 1000) ...

Slide 92

Slide 92 text

implicit conversions augmenting types with new operations ‘a’.toUpperCase

Slide 93

Slide 93 text

implicit conversions augmenting types with new operations implicit def charOps(c: Char) = new { def toUpperCase = if (c >= ‘a’ && c <= ‘z’) (c – 32).toChar else c } ... ‘a’.toUpperCase

Slide 94

Slide 94 text

Pimp my library

Slide 95

Slide 95 text

implicit conversions pimping your libraries since 2006 import scalaj.collection._ val list = new java.util.ArrayList[Int] list.add(1) list.add(2) list.add(3) ... for (x <- list) yield x * 2

Slide 96

Slide 96 text

implicit conversions pimping your libraries since 2006 import scalaj.collection._ val list = new java.util.ArrayList[Int] list.add(1) list.add(2) list.add(3) ... for (x <- list) yield x * 2 // list.map(x => x * 2)

Slide 97

Slide 97 text

implicit conversions pimping your libraries since 2006 import scalaj.collection._ val list = new java.util.ArrayList[Int] list.add(1) list.add(2) list.add(3) ... for (x <- list) yield x * 2 // jlist2slist(list).map(x => x * 2)

Slide 98

Slide 98 text

implicit arguments restricting operations on types val is = SortedSet(1, 2, 3) case class Man(id: Int) ... implicit object MenOrd extends Ordering[Man] { def compare(x: Man, y: Man) = x.id – y.id } val ms = SortedSet(Person(1), Person(2))

Slide 99

Slide 99 text

STM software transactional memory val account1 = cell[Int] val account2 = cell[Int] atomic { implicit txn => if (account2() >= 50) { account1 += 50 account2 -= 50 } }

Slide 100

Slide 100 text

Scala has support.

Slide 101

Slide 101 text

Building Ant SBT Maven

Slide 102

Slide 102 text

SBT simple build tool > compile ... > ~compile ... > test ... > package

Slide 103

Slide 103 text

SBT project definition written in Scala val scalatest = “org.scala-tools.testing” % “scalatest” % “0.9.5” ... > reload [INFO] Building project ... [INFO] using sbt.Default ... > update

Slide 104

Slide 104 text

Tools: IDEs Eclipse Netbeans IntelliJ IDEA Emacs ENSIME JEdit

Slide 105

Slide 105 text

Web frameworks

Slide 106

Slide 106 text

Used by...

Slide 107

Slide 107 text

Thank you!