SlideShare a Scribd company logo
1 of 107
Download to read offline
Introduction to   Scala Aleksandar Prokopec EPFL
 
Pragmatic Since 2003 runs on the  JVM Seamless Java interoperability Statically  typed Production ready Martin Odersky Hybrid
Statically  typed
runs on the   JVM
Scala  programs are fast.
OOP   +  FP
“ 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
“ If I were to pick a language to use today other than Java, it would be  Scala .” James Gosling
Pragmatic
Scala  is lightweight.
println( “Hello world!” )
scala> println( “Hello world!” ) Hello world! REPL evaluating expressions on the fly
object  MyApp  extends  App { println( “Hello world!” ) } Compiled version
object  MyApp  extends  App { println( “Hello world!” ) } Singleton  object s no more static methods
object  MyApp { def  main(args: Array[String]) { println( “Hello world!” ) } } Declaring methods
object  MyApp { def  main(args: Array[String]) { var  user: String = args(0) println( “Hello, ” +user+ “!” ) } } Declaring  var iables
object  MyApp { def  main(args: Array[String]) { val  user: String = args(0) println( “Hello, ” +user+ “!” ) } } Declaring  val ues prevents accidental changes
object  MyApp { def  main(args: Array[String]) { val  user = args(0) println( “Hello, ” +user+ “!” ) } } Local type inference less… “typing”
class  StringArrayFactory { def  create : Array[String]  = { val  array:  Array[String]  = Array [String] ( “1” ,  “2” ,  “3” ) array } } Local type inference less… “typing”
class  StringArrayFactory { def  create = { val  array = Array( “1” ,  “2” ,  “3” ) array } } Local type inference less… “typing”
// Scala class  Person( var  name: String, var  age: Int ) Declaring  class es … 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; } }
Scala  is object-oriented.
object   Foo   { val  b =  new  ArrayBuffer[Any] } Object-oriented everything’s an object
Object-oriented everything’s an object Any AnyRef AnyVal String Boolean Char Int
object   Foo   { val  b =  new  ArrayBuffer[Any] b += 1 b += 1.toString b += Foo println(b) } Object-oriented everything’s an object
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
trait   Iterator[T]   { def  next(): T def  hasNext: Boolean } Declaring  trait s traits are like interfaces
trait   Iterator[T]   { def  next(): T def  hasNext: Boolean def   printAll() = while   (hasNext) println(next()) } Declaring  trait s traits are rich
trait   Animal Multiple inheritance traits are composable
trait   Animal trait   Mammal   extends   Animal { def  think() = println( “hm...” ) } Multiple inheritance traits are composable
trait   Animal trait   Mammal   extends   Animal { def  think() = println( “hm...” ) } trait   Bird   extends   Animal { def  layEgg() = System.createEgg() } Multiple inheritance traits are composable
 
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
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”
Cake pattern
trait   Logging { def  log(msg: String) } trait  AnsweringMachine { self: Logging  with  DAO  with  Protocol  => log( “Initializing.” ) ... } Self -types to express requirements
trait  ConsoleLogging { def   log(msg: String) = println(msg) } class  LocalAnsweringMachine extends  AnsweringMachine with  ConsoleLogging with  H2DAO with  JabberProtocol Cake pattern layers above layers
Scala  is functional.
(x: Int)  =>  x + 1 First class functions
val  doub : Int => Int  = (x: Int)  =>  x * 2 doub(1)    2 First class  functions functions are objects too
val  doub = (x: Int)  =>  x * 2 List(1, 2, 3).map(doub)    List(2, 4, 6) First class functions as higher order parameters
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
var  step = 1 val  inc = x  =>  x + step inc(5)    6 step = 2 inc(5)    7 Closures functions that “capture” their environment
// 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
Pattern matching
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
trait  Tree case class  Node(l: Tree, r: Tree) extends  Tree case object  Leaf extends  Tree Pattern  matching … is precise
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
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
def  matchingMeSoftly(a: Any): Any = a  match  { case  11  =>   “eleven” case  s: String  =>   “’%s’” .format(s) case   <tag> {t} </tag>   =>  t case  Array(1, 2, 3)  =>  “1, 2, 3” case  head :: tail   =>  tail case  _  => null } Pattern matching … is extensible
Lazyness
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)) ) }
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” ) }
Stream s lazy lists e = ∑ 1/n!   = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + …
Stream s lazy lists e = ∑ 1/n!   = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + … 0! ?
Stream s lazy lists e = ∑ 1/n!   = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + … 0! 1! 2! 3! ? 0! ?
Stream s 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! ?
Stream s 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
Scala  is expressive.
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))
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)
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
Collections easy to create val   phonebook = Map( “ Jean”  ->   “123456” , “ Damien”  ->  “666666” ) val  meetings = ArrayBuffer( “ Dante” ,  “Damien” ,  “Sophie” ) println(phonebook(meetings(1)))
Collections high-level combinators // Java boolean  isOk = true for  ( int  i = 0; i < name.length(); i++) { if  (isLetterOrDigit(name.charAt(i)) { isOk =  false ; break ; } }
Collections high-level combinators // Scala name.forall(_.isLetterOrDigit)
Collections high-level combinators // count the total number of different // surnames shared by at least 2 adults people
Collections high-level combinators // count the total number of different // surnames shared by at least 2 adults people.filter(_.age >= 18)
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]]
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 }
List s an immutable sequence val  countdown = List(3, 2, 1) 3 2 1 countdown
List s an immutable sequence val  countdown = List(3, 2, 1) val  longer = 4 :: countdown 3 2 1 4 countdown longer
List s 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
List s 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
Buffer s mutable sequences val  b = ArrayBuffer(1, 2, 3) b += 4 b += 5 b += 6    ArrayBuffer(1, 2, 3, 4, 5, 6)
Map s 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)
Hash tries persistence through efficient structural sharing val   im0: Map[Int, Int] =  ... im0
Hash tries persistence through efficient structural sharing val   im0: Map[Int, Int] =  ... val  im1 = im0 + (1 -> 1) im0 im1
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
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
Hash tries persistence through efficient structural sharing im0 im1 im2 im3 2x-3x slower lookup 2x faster iteration
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
Scala  is extensible.
One ring to rule them all.
actor s road to safer concurrency val  a = actor { react { case  i: Int  =>  println(i) } } ... a ! 5
Custom control flow it’s all about control def  myWhile(c:  => Boolean)(b:  => Unit) { if  (c) { b myWhile(c)(b) } }
Custom control flow it’s all about control @tailrec def  myWhile(c:  => Boolean)(b:  => Unit) { if (c) { b myWhile(c)(b) } }
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 }
ARM automatic resource management withFile ( “~/.bashrc” ) { f  => for  (l <- f.lines) { if  ( “#” .r.findFirstIn(l) != None) println(l) } }
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) } }
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) ...
implicit  conversions augmenting types with new operations ‘ a’ .toUpperCase
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
Pimp  my library
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
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)
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)
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))
STM software transactional memory val  account1 = cell[Int] val  account2 = cell[Int] atomic {  implicit  txn  => if  (account2() >= 50) { account1 += 50 account2 -= 50 } }
Scala  has support.
Building Ant SBT Maven
SBT simple build tool > compile ... > ~compile ... > test ... > package
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
Tools:  IDEs Eclipse Netbeans IntelliJ IDEA Emacs ENSIME JEdit
Web frameworks
Used by...
Thank you!

More Related Content

What's hot

Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)intelliyole
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Cody Engel
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1Hackraft
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation streamRuslan Shevchenko
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functionalHackraft
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in ScalaShai Yallin
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and ClosuresSandip Kumar
 
ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021Jorge Vásquez
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Jorge Vásquez
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonUC San Diego
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Derek Chen-Becker
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaOstap Andrusiv
 

What's hot (20)

Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Into Clojure
Into ClojureInto Clojure
Into Clojure
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functional
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in Scala
 
Sneaking inside Kotlin features
Sneaking inside Kotlin featuresSneaking inside Kotlin features
Sneaking inside Kotlin features
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
 
ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with Scala
 
Scala
ScalaScala
Scala
 

Viewers also liked

Work-stealing Tree Data Structure
Work-stealing Tree Data StructureWork-stealing Tree Data Structure
Work-stealing Tree Data StructureAleksandar Prokopec
 
An Introduct to Spark - Atlanta Spark Meetup
An Introduct to Spark - Atlanta Spark MeetupAn Introduct to Spark - Atlanta Spark Meetup
An Introduct to Spark - Atlanta Spark Meetupjlacefie
 
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)Evan Chan
 
The Future of Data Science
The Future of Data ScienceThe Future of Data Science
The Future of Data Sciencesarith divakar
 
Spark 101 - First steps to distributed computing
Spark 101 - First steps to distributed computingSpark 101 - First steps to distributed computing
Spark 101 - First steps to distributed computingDemi Ben-Ari
 
Pixie dust overview
Pixie dust overviewPixie dust overview
Pixie dust overviewDavid Taieb
 
Spark tutorial py con 2016 part 2
Spark tutorial py con 2016   part 2Spark tutorial py con 2016   part 2
Spark tutorial py con 2016 part 2David Taieb
 
Spark tutorial pycon 2016 part 1
Spark tutorial pycon 2016   part 1Spark tutorial pycon 2016   part 1
Spark tutorial pycon 2016 part 1David Taieb
 
Data Science with Spark - Training at SparkSummit (East)
Data Science with Spark - Training at SparkSummit (East)Data Science with Spark - Training at SparkSummit (East)
Data Science with Spark - Training at SparkSummit (East)Krishna Sankar
 
A Step to programming with Apache Spark
A Step to programming with Apache SparkA Step to programming with Apache Spark
A Step to programming with Apache SparkKnoldus Inc.
 
Scala meetup - Intro to spark
Scala meetup - Intro to sparkScala meetup - Intro to spark
Scala meetup - Intro to sparkJavier Arrieta
 
Scaling out logistic regression with Spark
Scaling out logistic regression with SparkScaling out logistic regression with Spark
Scaling out logistic regression with SparkBarak Gitsis
 
Big data processing with apache spark
Big data processing with apache sparkBig data processing with apache spark
Big data processing with apache sparksarith divakar
 
Apache Spark Tutorial
Apache Spark TutorialApache Spark Tutorial
Apache Spark TutorialAhmet Bulut
 
Meet Up - Spark Stream Processing + Kafka
Meet Up - Spark Stream Processing + KafkaMeet Up - Spark Stream Processing + Kafka
Meet Up - Spark Stream Processing + KafkaKnoldus Inc.
 
Spark Summit EU talk by Sameer Agarwal
Spark Summit EU talk by Sameer AgarwalSpark Summit EU talk by Sameer Agarwal
Spark Summit EU talk by Sameer AgarwalSpark Summit
 
Spark Summit EU 2015: Lessons from 300+ production users
Spark Summit EU 2015: Lessons from 300+ production usersSpark Summit EU 2015: Lessons from 300+ production users
Spark Summit EU 2015: Lessons from 300+ production usersDatabricks
 
Akka Finite State Machine
Akka Finite State MachineAkka Finite State Machine
Akka Finite State MachineKnoldus Inc.
 

Viewers also liked (20)

Ctrie Data Structure
Ctrie Data StructureCtrie Data Structure
Ctrie Data Structure
 
Work-stealing Tree Data Structure
Work-stealing Tree Data StructureWork-stealing Tree Data Structure
Work-stealing Tree Data Structure
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
An Introduct to Spark - Atlanta Spark Meetup
An Introduct to Spark - Atlanta Spark MeetupAn Introduct to Spark - Atlanta Spark Meetup
An Introduct to Spark - Atlanta Spark Meetup
 
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
Spark Job Server and Spark as a Query Engine (Spark Meetup 5/14)
 
The Future of Data Science
The Future of Data ScienceThe Future of Data Science
The Future of Data Science
 
Spark 101 - First steps to distributed computing
Spark 101 - First steps to distributed computingSpark 101 - First steps to distributed computing
Spark 101 - First steps to distributed computing
 
Pixie dust overview
Pixie dust overviewPixie dust overview
Pixie dust overview
 
Spark tutorial py con 2016 part 2
Spark tutorial py con 2016   part 2Spark tutorial py con 2016   part 2
Spark tutorial py con 2016 part 2
 
Spark tutorial pycon 2016 part 1
Spark tutorial pycon 2016   part 1Spark tutorial pycon 2016   part 1
Spark tutorial pycon 2016 part 1
 
Data Science with Spark - Training at SparkSummit (East)
Data Science with Spark - Training at SparkSummit (East)Data Science with Spark - Training at SparkSummit (East)
Data Science with Spark - Training at SparkSummit (East)
 
A Step to programming with Apache Spark
A Step to programming with Apache SparkA Step to programming with Apache Spark
A Step to programming with Apache Spark
 
Scala meetup - Intro to spark
Scala meetup - Intro to sparkScala meetup - Intro to spark
Scala meetup - Intro to spark
 
Scaling out logistic regression with Spark
Scaling out logistic regression with SparkScaling out logistic regression with Spark
Scaling out logistic regression with Spark
 
Big data processing with apache spark
Big data processing with apache sparkBig data processing with apache spark
Big data processing with apache spark
 
Apache Spark Tutorial
Apache Spark TutorialApache Spark Tutorial
Apache Spark Tutorial
 
Meet Up - Spark Stream Processing + Kafka
Meet Up - Spark Stream Processing + KafkaMeet Up - Spark Stream Processing + Kafka
Meet Up - Spark Stream Processing + Kafka
 
Spark Summit EU talk by Sameer Agarwal
Spark Summit EU talk by Sameer AgarwalSpark Summit EU talk by Sameer Agarwal
Spark Summit EU talk by Sameer Agarwal
 
Spark Summit EU 2015: Lessons from 300+ production users
Spark Summit EU 2015: Lessons from 300+ production usersSpark Summit EU 2015: Lessons from 300+ production users
Spark Summit EU 2015: Lessons from 300+ production users
 
Akka Finite State Machine
Akka Finite State MachineAkka Finite State Machine
Akka Finite State Machine
 

Similar to Introduction to the Scala Programming Language

Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scalaparag978978
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)William Narmontas
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala LanguageAshal aka JOKER
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うbpstudy
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersMatthew Farwell
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersTikal Knowledge
 

Similar to Introduction to the Scala Programming Language (20)

Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
 
An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使う
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 

Recently uploaded

Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Recently uploaded (20)

Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

Introduction to the Scala Programming Language

  • 1. Introduction to Scala Aleksandar Prokopec EPFL
  • 2.  
  • 3. Pragmatic Since 2003 runs on the JVM Seamless Java interoperability Statically typed Production ready Martin Odersky Hybrid
  • 6. Scala programs are fast.
  • 7. OOP + FP
  • 8. “ 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
  • 9. “ If I were to pick a language to use today other than Java, it would be Scala .” James Gosling
  • 11. Scala is lightweight.
  • 13. scala> println( “Hello world!” ) Hello world! REPL evaluating expressions on the fly
  • 14. object MyApp extends App { println( “Hello world!” ) } Compiled version
  • 15. object MyApp extends App { println( “Hello world!” ) } Singleton object s no more static methods
  • 16. object MyApp { def main(args: Array[String]) { println( “Hello world!” ) } } Declaring methods
  • 17. object MyApp { def main(args: Array[String]) { var user: String = args(0) println( “Hello, ” +user+ “!” ) } } Declaring var iables
  • 18. object MyApp { def main(args: Array[String]) { val user: String = args(0) println( “Hello, ” +user+ “!” ) } } Declaring val ues prevents accidental changes
  • 19. object MyApp { def main(args: Array[String]) { val user = args(0) println( “Hello, ” +user+ “!” ) } } Local type inference less… “typing”
  • 20. class StringArrayFactory { def create : Array[String] = { val array: Array[String] = Array [String] ( “1” , “2” , “3” ) array } } Local type inference less… “typing”
  • 21. class StringArrayFactory { def create = { val array = Array( “1” , “2” , “3” ) array } } Local type inference less… “typing”
  • 22. // Scala class Person( var name: String, var age: Int ) Declaring class es … 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; } }
  • 23. Scala is object-oriented.
  • 24. object Foo { val b = new ArrayBuffer[Any] } Object-oriented everything’s an object
  • 25. Object-oriented everything’s an object Any AnyRef AnyVal String Boolean Char Int
  • 26. object Foo { val b = new ArrayBuffer[Any] b += 1 b += 1.toString b += Foo println(b) } Object-oriented everything’s an object
  • 27. 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
  • 28. trait Iterator[T] { def next(): T def hasNext: Boolean } Declaring trait s traits are like interfaces
  • 29. trait Iterator[T] { def next(): T def hasNext: Boolean def printAll() = while (hasNext) println(next()) } Declaring trait s traits are rich
  • 30. trait Animal Multiple inheritance traits are composable
  • 31. trait Animal trait Mammal extends Animal { def think() = println( “hm...” ) } Multiple inheritance traits are composable
  • 32. trait Animal trait Mammal extends Animal { def think() = println( “hm...” ) } trait Bird extends Animal { def layEgg() = System.createEgg() } Multiple inheritance traits are composable
  • 33.  
  • 34. 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
  • 35. 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”
  • 37. trait Logging { def log(msg: String) } trait AnsweringMachine { self: Logging with DAO with Protocol => log( “Initializing.” ) ... } Self -types to express requirements
  • 38. trait ConsoleLogging { def log(msg: String) = println(msg) } class LocalAnsweringMachine extends AnsweringMachine with ConsoleLogging with H2DAO with JabberProtocol Cake pattern layers above layers
  • 39. Scala is functional.
  • 40. (x: Int) => x + 1 First class functions
  • 41. val doub : Int => Int = (x: Int) => x * 2 doub(1)  2 First class functions functions are objects too
  • 42. val doub = (x: Int) => x * 2 List(1, 2, 3).map(doub)  List(2, 4, 6) First class functions as higher order parameters
  • 43. 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
  • 44. var step = 1 val inc = x => x + step inc(5)  6 step = 2 inc(5)  7 Closures functions that “capture” their environment
  • 45. // 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
  • 47. 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
  • 48. trait Tree case class Node(l: Tree, r: Tree) extends Tree case object Leaf extends Tree Pattern matching … is precise
  • 49. 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
  • 50. 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
  • 51. def matchingMeSoftly(a: Any): Any = a match { case 11 => “eleven” case s: String => “’%s’” .format(s) case <tag> {t} </tag> => t case Array(1, 2, 3) => “1, 2, 3” case head :: tail => tail case _ => null } Pattern matching … is extensible
  • 53. 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)) ) }
  • 54. 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” ) }
  • 55. Stream s lazy lists e = ∑ 1/n! = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + …
  • 56. Stream s lazy lists e = ∑ 1/n! = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + … 0! ?
  • 57. Stream s lazy lists e = ∑ 1/n! = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + … 0! 1! 2! 3! ? 0! ?
  • 58. Stream s 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! ?
  • 59. Stream s 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
  • 60. Scala is expressive.
  • 61. 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))
  • 62. 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)
  • 63. 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
  • 64. Collections easy to create val phonebook = Map( “ Jean” -> “123456” , “ Damien” -> “666666” ) val meetings = ArrayBuffer( “ Dante” , “Damien” , “Sophie” ) println(phonebook(meetings(1)))
  • 65. Collections high-level combinators // Java boolean isOk = true for ( int i = 0; i < name.length(); i++) { if (isLetterOrDigit(name.charAt(i)) { isOk = false ; break ; } }
  • 66. Collections high-level combinators // Scala name.forall(_.isLetterOrDigit)
  • 67. Collections high-level combinators // count the total number of different // surnames shared by at least 2 adults people
  • 68. Collections high-level combinators // count the total number of different // surnames shared by at least 2 adults people.filter(_.age >= 18)
  • 69. 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]]
  • 70. 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 }
  • 71. List s an immutable sequence val countdown = List(3, 2, 1) 3 2 1 countdown
  • 72. List s an immutable sequence val countdown = List(3, 2, 1) val longer = 4 :: countdown 3 2 1 4 countdown longer
  • 73. List s 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
  • 74. List s 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
  • 75. Buffer s mutable sequences val b = ArrayBuffer(1, 2, 3) b += 4 b += 5 b += 6  ArrayBuffer(1, 2, 3, 4, 5, 6)
  • 76. Map s 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)
  • 77. Hash tries persistence through efficient structural sharing val im0: Map[Int, Int] = ... im0
  • 78. Hash tries persistence through efficient structural sharing val im0: Map[Int, Int] = ... val im1 = im0 + (1 -> 1) im0 im1
  • 79. 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
  • 80. 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
  • 81. Hash tries persistence through efficient structural sharing im0 im1 im2 im3 2x-3x slower lookup 2x faster iteration
  • 82. 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
  • 83. Scala is extensible.
  • 84. One ring to rule them all.
  • 85. actor s road to safer concurrency val a = actor { react { case i: Int => println(i) } } ... a ! 5
  • 86. Custom control flow it’s all about control def myWhile(c: => Boolean)(b: => Unit) { if (c) { b myWhile(c)(b) } }
  • 87. Custom control flow it’s all about control @tailrec def myWhile(c: => Boolean)(b: => Unit) { if (c) { b myWhile(c)(b) } }
  • 88. 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 }
  • 89. ARM automatic resource management withFile ( “~/.bashrc” ) { f => for (l <- f.lines) { if ( “#” .r.findFirstIn(l) != None) println(l) } }
  • 90. 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) } }
  • 91. 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) ...
  • 92. implicit conversions augmenting types with new operations ‘ a’ .toUpperCase
  • 93. 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
  • 94. Pimp my library
  • 95. 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
  • 96. 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)
  • 97. 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)
  • 98. 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))
  • 99. STM software transactional memory val account1 = cell[Int] val account2 = cell[Int] atomic { implicit txn => if (account2() >= 50) { account1 += 50 account2 -= 50 } }
  • 100. Scala has support.
  • 102. SBT simple build tool > compile ... > ~compile ... > test ... > package
  • 103. 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
  • 104. Tools: IDEs Eclipse Netbeans IntelliJ IDEA Emacs ENSIME JEdit