SlideShare a Scribd company logo
Scala: Object-Oriented 
meets Functional 
An overview of the Scala programming language 
Iulian Dragos 
1
What is Scala? 
•A programming language running on the JVM 
(also a JS backend) 
•Statically typed, combines object-orientation 
and functional programming 
•Concise 
•Fully interoperable with Java 
•As fast as Java 
2
History 
•“Make Java better” 
•1997 - Pizza and GJ (Odersky, Wadler) 
•2000 - Java 5 (generics) 
•“Make a better Java” 
•first release 2004 
•adoption begins 2007 
•first Scala conference 2008 
3
Scala drives its social graph service: 
380-400 M transactions/day 
Migrated core messaging service 
from Ruby to sustain 1000x growth 
Approved for general production 
use 
Location-based social network. All 
written in Scala (>5M users). 
5
Philosophy 
• “Growable language” (Guy Steele) 
• embed DSLs/add new types 
• Scalable language 
• same concepts in small and large applications 
• Deep rather than broad 
• focus on abstraction and composition 
6
public class Person { 
public final String name; 
public final int age; 
Person(String name, int age) { 
this.name = name; 
this.age = age; 
} 
} 
class Person(val name: String, 
val age: Int) 
Java 
Scala 
7
import java.util.ArrayList; 
... 
Person[] people; 
Person[] minors; 
Person[] adults; 
{ ArrayList<Person> minorsList = new ArrayList<Person>(); 
ArrayList<Person> adultsList = new ArrayList<Person>(); 
for (int i = 0; i < people.length; i++) 
(people[i].age < 18 ? minorsList : adultsList) 
.add(people[i]); 
minors = minorsList.toArray(people); 
adults = adultsList.toArray(people); 
} 
val people: Array[Person] 
val (minors, adults) = people partition (_.age < 18) 
A tuple in a pattern match 
Infix method call 
Anonymous function 
8
Overview 
•Scala as an Object-Oriented Language 
•Scala as a Functional Programming 
Language 
•Scala as a host language for DSLs 
9
Scala Basics 
10
Running Scala 
•Compiled to Java bytecode 
•Read-Eval-Print-Loop (REPL) 
•fast turnaround 
•easy experimentation/testing 
11
The bottom line 
•Every value is an object 
•Everything is an expression (evaluates to 
a value) 
•Every operation is a method call 
•What about primitives? 
12
type can be inferred 
val x: Int = 10 
val y = x + 10 
same as x.+(10) 
13
final class Int { 
def +(y: Int): Int = <native> 
def -(y: Int): Int = <native> 
def *(y: Int): Int = <native> 
def /(y: Int): Int = <native> 
... 
} 
The compiler treats all primitives as if they were instances of 
such classes (but uses primitive values for performance) 
14
class Complex(val re: Int, val im: Int) { 
def +(that: Complex) = 
new Complex(this.re + that.re, this.im + that.im) 
// .. 
override def toString = 
"%d + %di".format(re, im) 
} 
15
scala> val c = new Complex(1, 2) 
c: test.Complex = 1 + 2i 
scala> val c1 = new Complex(2, 2) 
c1: test.Complex = 2 + 2i 
scala> c + c1 
res0: test.Complex = 3 + 4i 
16
Everything is an 
expression 
•No statements 
•Reduces the need for return and side-effects 
17
def max(x: Int, y: Int) = 
if (x > y) x else y 
no return statement 
scala> val t = { 
| x = x + 10 
| x - 1 
| } 
t: Int = 9 
return type inferred to Int 
blocks evaluate to last 
expression 
18
What about println? 
scala> val x = println("hello, world") 
hello, world 
x: Unit = () 
The Unit type is like void in Java: no interesting value 
19
Classes and Traits 
20
Classes 
•Similar to Java classes 
•have fields, methods, parameters and 
types 
•every member can be overridden 
•except classes (no virtual classes) 
•any member can be abstract 
21
explicit abstract class parameters/fields 
abstract class Node[T](val v: T, next: Node[T]) 
extends List(v, next) { 
val cache: Int 
def first: T 
def children = List(next) 
override def toString = "Node " + v 
} 
22 
super ctor call 
When compiled, this class will look and behave exactly like the 
obvious translation to a Java class. No glue code necessary.
Traits 
•Like Java interfaces, but in addition 
•allow concrete methods, fields, types 
•Like Scala classes, but without 
constructor parameters 
•Allow (a form of) multiple inheritance 
•mix-in composition 
23
type parameter can extend Java classes 
trait Ordered[A] extends java.lang.Comparable[A] { 
def compare(that: A): Int 
def < (that: A): Boolean = (this compare that) < 0 
def > (that: A): Boolean = (this compare that) > 0 
def <= (that: A): Boolean = (this compare that) <= 0 
def >= (that: A): Boolean = (this compare that) >= 0 
} 
classes mixing in Ordered will get all these methods ‘for free’ 
class Complex extends Ordered[Complex] { 
def compare(that: Complex): Int = ... 
} 
24
if (c1 > c2) c1 else c2 
val xs: List[Complex] 
xs.sorted 
25
Mix-in composition 
26
Mix-in example: Cells 
•model mutable cells that contain a single 
value of some arbitrary type. 
•Additionally, define logging and 
undoable variants. 
27
A class for generic cells 
class Cell[T](val init: T) { 
private var v = init 
def get(): T = v 
def set(v1: T): Unit = { v = v1 } 
override def toString: String = 
"Cell("+ v +")" 
} 
make init available as a field 
mutable field 
28
A trait for undoable cells 
trait UndoableCell[T] extends Cell[T] { 
import mutable.ArrayStack 
private var hist = new ArrayStack[T]() 
override def set(v1: T): Unit = { 
hist.push(super.get()) 
super.set(v1) 
} 
def undo(): Unit = 
super.set(hist pop) 
} 
29
A trait for logging cells 
trait LoggingCell[T] extends Cell[T] { 
override def get(): T = { 
println("getting "+ this) 
super.get() 
} 
override def set(v1: T): Unit = { 
println("setting "+ this +" to "+ v1) 
super.set(v1) 
} 
} 
30
Mix-in composition 
new Cell(0) 
new Cell(0) 
with LoggingCell[Int] 
new Cell(0) 
with LoggingCell[Int] 
with UndoableCell[Int] 
new Cell(0) 
with UndoableCell[Int] 
with LoggingCell[Int] 
cell with logging 
cell with logging 
and undoing 
cell with undoing 
and logging 
31
Mix-in composition 
•traits can be stacked 
•the call to super is dynamically bound 
•super calls the ‘preceding’ 
implementation in the instantiated object 
32
Late binding for super 
33 
Cell 
myCell 
Undo Logging
Modules 
•Scala uses objects and traits for module 
composition 
•Composition through mixins 
•Modules may require another module 
34
Customers and DB 
•Module Accounting 
•requires a logger, a customer service 
•Module Customers 
•requires a logger, provides the 
customer service 
35
trait Accounting { this: Customers => 
val logger: Logger 
customers.getCustomers // .. 
} 
trait Customers { 
val logger: Logger 
val customers: CustomerService 
class CustomerService { 
requires module Customers 
def getCustomers: Unit = () 
//... 
} 
} 
36
object App extends Accounting 
with Customers { 
val logger = // logback 
val customers = new CustomerService 
} 
The application uses (some) concrete implementations of Accounting 
and CustomerService. 
37
This gives dependency 
injection in the language! 
object TestApp extends Accounting 
with Customers { 
val logger = // println based 
val customers = new MockCustomerService 
} 
The test environment uses a mock CustomerService. 
38
Summary 
•Basic O-O features 
•classes and traits 
•type inference 
•mix-in composition 
•Advanced type system 
•modules and dependencies 
•dynamic binding of super 
39
Functional 
Programming 
40
What is FP? 
•Use of functions (in the mathematical sense) 
•referential transparency (no side-effects) 
•Immutable objects 
•Functions are values 
41
Scala as FPL 
•function literals and closures 
•use val instead of var 
•immutable collections in the standard 
library 
•opt-in lazy evaluation 
•curry-ing (definition site) 
42
Function literals 
scala> val succ = (x: Int) => x + 1 
succ: (Int) => Int = <function1> 
scala> succ(1) 
res3: Int = 2 
43
Equivalent forms 
•x => x + 1 (infer type) 
•1 + (partial application) 
•_ + 1 (placeholder notation) 
•compare _ (eta expansion) 
44
Higher-Order 
Functions 
•functions that take or return functions 
•almost eliminate the need for loops over 
collections 
45
val xs = List(1, 2, 3, 4, 5) 
xs.foreach(println) 
scala> xs.forall(_ < 10) 
res5: Boolean = true 
scala> xs.map(_ * 2) 
res6: List[Int] = List(2, 4, 6, 8, 10) 
def findBelow(limit: Int) = 
persons.filter(_.age < limit) 
lexical scoping: functions may refer and 
even modify anything in scope 
46
Everything is an object 
•Closures are objects as well 
•(compiler generated) instances of trait 
Function1[A, B] 
trait Function1[R, A] { 
def apply(x: A): R 
} 
47
(More) sugar 
•..but why can I call succ(10)? 
•f(args) is desugared to f.apply(args) 
•you can define your own ‘apply’ 
methods 
•..can I subclass FunctionN? 
48
Library 
•Yes! Lots of collections are functions 
•Sequences are Int => T 
•Sets are T => Boolean 
•Maps are K => V 
49
Standard Library 
50
Collections 
•Generic (List[T], Map[K, V]) 
•Mutable and immutable 
implementations (default is immutable) 
•Uniform (same method protocol for all 
collections) 
•Uniform Return Principle 
•No compiler magic! 
51
Example: Maps 
scala> val capitals = Map("France" -> "Paris", "Switzerland" -> "Bern", "Sweden" 
-> "Stockholm") 
capitals: immutable.Map[String,String] = Map(France -> Paris, Switzerland -> 
Bern, Sweden -> Stockholm) 
scala> capitals("France") 
res7: java.lang.String = Paris 
scala> capitals + ("Romania" -> "Bucharest") 
res8: immutable.Map[String,String] = Map(France -> Paris, Switzerland -> Bern, 
Sweden -> Stockholm, Romania -> Bucharest) 
scala> capitals 
res9: immutable.Map[String,String] = Map(France -> Paris, Switzerland -> Bern, 
Sweden -> Stockholm) 
scala> capitals.filter(_._2 == "Paris") 
res10: immutable.Map[String,String] = Map(France -> Paris) 
52
For-comprehensions 
•More general than for-loops 
•Used to iterate, filter and generate new 
collections 
53
For-comprehensions 
may have any number of generators 
p is in scope for other generators 
for (p <- persons; pr <- p.projects; 
if pr.overdue) yield p.name 
guard construct a new collection of the same 
type, element by element 
res10: List[String] = List(“John”, “Harry”) 
54
For-comprehensions 
•Desugared to calls to filter, map, flatMap 
•..any class implementing those methods 
•Could be used to query a database 
•implement filter, map, flatmap to 
generate and forward SQL 
•Slick, ScalaQuery, Squeryl 
55
Pattern matching 
56
Pattern matching 
•A way to deconstruct structured data 
•A powerful switch statement (expression, 
really) 
57
Pattern matching 
abstract class Expr 
case class Literal(n: Int) extends Expr 
case class Add(left: Expr, right: Expr) 
extends Expr 
case class Mul(left: Expr, right: Expr) 
extends Expr the case modifier enables these classes 
to participate in pattern matching 
58
Pattern matching 
def opt(e: Expr) = e match { 
case Add(l, Literal(0)) => l 
case Mul(l, Literal(1)) => l 
case _ => e 
} 
patterns can be nested 
literals match only their value 
default case 
59
Patterns everywhere 
•Great way to process XML, JSON, etc. 
•exception handlers are also patterns! 
•..and also declarations 
60
try { 
//... 
} catch { 
case EvalError(line, msg) => //.. 
case e: IndexOutOfBoundsException => //.. 
} 
61
Embedding DSLs 
62
Implicit conversions 
•We can “grow” the language with 
Complex numbers 
•c + 1 works, but what about 1 + c? 
63
Implicit conversions 
•Allow user-defined conversions 
•When the type does not have a member 
•Find a method marked implicit that 
takes the original type to one that has it 
•Call the method implicitly 
•It’s an error to have more than one 
applicable conversions 
64
implicit def intIsComplex(n: Int) = 
new RichInt(n) 
class RichInt(n: Int) { 
def + (other: Complex) = 
new Complex(n) + other 
} 
1 + c intIsComplex(1).+(c) 
65
implicit class RichInt(n: Int) { 
def + (other: Complex) = 
new Complex(n) + other 
} 
Also a great way to adapt existing libraries 
66
implicit class RichArray[T](as: Array[T]) { 
def foreach(f: T => Unit) = ??? 
def map[U](f: T => U) = ??? 
def filter(f: T => Boolean) = ??? 
// ... 
} 
Scala arrays are Java arrays, but can still have the same API 
67
Actor-based concurrency 
•Threads and locks are error-prone 
•(in fact, mutable shared data) 
•Alternative: Message-passing with actors 
•(no sharing) 
•Implemented entirely in the library 
•(3 other implementations out there) 
68
Actors in Scala 
actor ! msg 
asynchronous message send 
method call 
receive { 
case Ping(a1) => a1 ! msg 
case Pong(_) => .. 
} 
Normal pattern match translated 
to PartialFunction 
69
What else? 
70
Lots 
•Parallel collections 
library! 
•Staging 
the in •Call-by-name parameters 
•Specialization 
71
Where to start 
•Pick one of the books 
•Scala for the Impatient 
(Addison-Wesley) 
•Atomic Scala (Bruce 
Eckel) 
•Scala for Java refugees 
(blog) 
72
Thank you! 
73

More Related Content

What's hot

All about scala
All about scalaAll about scala
All about scala
Yardena Meymann
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
Derek Chen-Becker
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
Ruslan Shevchenko
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
Scala basic
Scala basicScala basic
Scala basic
Nguyen Tuan
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
Bert Van Vreckem
 
Quick introduction to scala
Quick introduction to scalaQuick introduction to scala
Quick introduction to scala
Mohammad Hossein Rimaz
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
fanf42
 
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
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
Alfonso Ruzafa
 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In Practice
Michiel Borkent
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of Scala
Martin Odersky
 

What's hot (16)

All about scala
All about scalaAll about scala
All about scala
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
camel-scala.pdf
camel-scala.pdfcamel-scala.pdf
camel-scala.pdf
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Scala basic
Scala basicScala basic
Scala basic
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
Scala
ScalaScala
Scala
 
Quick introduction to scala
Quick introduction to scalaQuick introduction to scala
Quick introduction to scala
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
Functional Programming In Practice
Functional Programming In PracticeFunctional Programming In Practice
Functional Programming In Practice
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of Scala
 

Viewers also liked

Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan HodorogPractical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan Hodorog
3Pillar Global
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league
 
Psychological Issues Within Law Enforcement
Psychological Issues Within Law EnforcementPsychological Issues Within Law Enforcement
Psychological Issues Within Law Enforcement
Doug Aaron
 
How to Avoid Getting Malware on Your Computer
How to Avoid Getting Malware on Your ComputerHow to Avoid Getting Malware on Your Computer
How to Avoid Getting Malware on Your Computer
Jillian Stone
 
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
3Pillar Global
 
El software y el hardware bb
El software y el  hardware bbEl software y el  hardware bb
El software y el hardware bbJhonatan Henao
 
Content is King
Content is KingContent is King
Content is King
3Pillar Global
 
Managing change in today's ever changing world of work
Managing change in today's ever changing world of workManaging change in today's ever changing world of work
Managing change in today's ever changing world of workCaleb Stick
 
Humor u rebt u
Humor u rebt uHumor u rebt u
Humor u rebt u
Dijana Sulejmanović
 
Cv 201503 eng_short
Cv 201503 eng_shortCv 201503 eng_short
Cv 201503 eng_short
Rolf Gundersen
 
How to Avoid Getting Malware on your Computer
How to Avoid Getting Malware on your ComputerHow to Avoid Getting Malware on your Computer
How to Avoid Getting Malware on your Computer
Jillian Stone
 
All-in-one monitoring solution for DevOps & IT
All-in-one monitoring solution for DevOps & ITAll-in-one monitoring solution for DevOps & IT
All-in-one monitoring solution for DevOps & IT
Rex Antony Peter
 
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
3Pillar Global
 
Introducing Packaging Textile Controller MAXVU
Introducing Packaging Textile Controller MAXVUIntroducing Packaging Textile Controller MAXVU
Introducing Packaging Textile Controller MAXVU
Kevin Anderson
 
Psychological Issues and the law
Psychological Issues and the lawPsychological Issues and the law
Psychological Issues and the law
Doug Aaron
 
Introducing the West Range
Introducing the West RangeIntroducing the West Range
Introducing the West Range
Kevin Anderson
 
DevOps for Windows Admins
DevOps for Windows Admins DevOps for Windows Admins
DevOps for Windows Admins
Rex Antony Peter
 
DigitalRiverBrandsReport
DigitalRiverBrandsReportDigitalRiverBrandsReport
DigitalRiverBrandsReportKate Roe
 
Patrick Seguin Experience
Patrick Seguin ExperiencePatrick Seguin Experience
Patrick Seguin Experience
PatrickSeguin
 

Viewers also liked (20)

Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan HodorogPractical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan Hodorog
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Psychological Issues Within Law Enforcement
Psychological Issues Within Law EnforcementPsychological Issues Within Law Enforcement
Psychological Issues Within Law Enforcement
 
How to Avoid Getting Malware on Your Computer
How to Avoid Getting Malware on Your ComputerHow to Avoid Getting Malware on Your Computer
How to Avoid Getting Malware on Your Computer
 
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
 
El software y el hardware bb
El software y el  hardware bbEl software y el  hardware bb
El software y el hardware bb
 
Content is King
Content is KingContent is King
Content is King
 
Managing change in today's ever changing world of work
Managing change in today's ever changing world of workManaging change in today's ever changing world of work
Managing change in today's ever changing world of work
 
Humor u rebt u
Humor u rebt uHumor u rebt u
Humor u rebt u
 
Cv 201503 eng_short
Cv 201503 eng_shortCv 201503 eng_short
Cv 201503 eng_short
 
How to Avoid Getting Malware on your Computer
How to Avoid Getting Malware on your ComputerHow to Avoid Getting Malware on your Computer
How to Avoid Getting Malware on your Computer
 
All-in-one monitoring solution for DevOps & IT
All-in-one monitoring solution for DevOps & ITAll-in-one monitoring solution for DevOps & IT
All-in-one monitoring solution for DevOps & IT
 
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
 
KONSTANTINOS' EXPERIENCE
KONSTANTINOS' EXPERIENCEKONSTANTINOS' EXPERIENCE
KONSTANTINOS' EXPERIENCE
 
Introducing Packaging Textile Controller MAXVU
Introducing Packaging Textile Controller MAXVUIntroducing Packaging Textile Controller MAXVU
Introducing Packaging Textile Controller MAXVU
 
Psychological Issues and the law
Psychological Issues and the lawPsychological Issues and the law
Psychological Issues and the law
 
Introducing the West Range
Introducing the West RangeIntroducing the West Range
Introducing the West Range
 
DevOps for Windows Admins
DevOps for Windows Admins DevOps for Windows Admins
DevOps for Windows Admins
 
DigitalRiverBrandsReport
DigitalRiverBrandsReportDigitalRiverBrandsReport
DigitalRiverBrandsReport
 
Patrick Seguin Experience
Patrick Seguin ExperiencePatrick Seguin Experience
Patrick Seguin Experience
 

Similar to Scala: Object-Oriented Meets Functional, by Iulian Dragos

scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
JUG Lausanne
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
Eric Pederson
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Meetu Maltiar
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Xebia IT Architects
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
Miles Sabin
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into Scala
Nehal Shah
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
JAX London
 
(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
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
Shai Yallin
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
Adrian Spender
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?
Mario Camou Riveroll
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Rahul Jain
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 

Similar to Scala: Object-Oriented Meets Functional, by Iulian Dragos (20)

scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into Scala
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
 
(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?
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 

More from 3Pillar Global

Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
3Pillar Global
 
Design Sprints Presentation at NoVA UX
Design Sprints Presentation at NoVA UX Design Sprints Presentation at NoVA UX
Design Sprints Presentation at NoVA UX
3Pillar Global
 
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
3Pillar Global
 
Less, But Better - Dieter Rams' Principles of Good Design
Less, But Better - Dieter Rams' Principles of Good DesignLess, But Better - Dieter Rams' Principles of Good Design
Less, But Better - Dieter Rams' Principles of Good Design
3Pillar Global
 
A Prototyping Case Study at NoVA UX
A Prototyping Case Study at NoVA UX A Prototyping Case Study at NoVA UX
A Prototyping Case Study at NoVA UX
3Pillar Global
 
Automated Performance Testing for Desktop Applications by Ciprian Balea
Automated Performance Testing for Desktop Applications by Ciprian BaleaAutomated Performance Testing for Desktop Applications by Ciprian Balea
Automated Performance Testing for Desktop Applications by Ciprian Balea
3Pillar Global
 
Prototyping for Business Outcomes at ModevUX
Prototyping for Business Outcomes at ModevUXPrototyping for Business Outcomes at ModevUX
Prototyping for Business Outcomes at ModevUX
3Pillar Global
 
Prototyping Your Way to Better and Faster Outcomes
Prototyping Your Way to Better and Faster Outcomes Prototyping Your Way to Better and Faster Outcomes
Prototyping Your Way to Better and Faster Outcomes
3Pillar Global
 
Cloud Platforms for Java
Cloud Platforms for JavaCloud Platforms for Java
Cloud Platforms for Java
3Pillar Global
 
MoDev East 2012 Presentation on Product Modernization
MoDev East 2012 Presentation on Product ModernizationMoDev East 2012 Presentation on Product Modernization
MoDev East 2012 Presentation on Product Modernization
3Pillar Global
 
Visualizing Relationships: Journalistic Problems in a Digital Age
Visualizing Relationships: Journalistic Problems in a Digital AgeVisualizing Relationships: Journalistic Problems in a Digital Age
Visualizing Relationships: Journalistic Problems in a Digital Age
3Pillar Global
 
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global
 

More from 3Pillar Global (12)

Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
 
Design Sprints Presentation at NoVA UX
Design Sprints Presentation at NoVA UX Design Sprints Presentation at NoVA UX
Design Sprints Presentation at NoVA UX
 
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
 
Less, But Better - Dieter Rams' Principles of Good Design
Less, But Better - Dieter Rams' Principles of Good DesignLess, But Better - Dieter Rams' Principles of Good Design
Less, But Better - Dieter Rams' Principles of Good Design
 
A Prototyping Case Study at NoVA UX
A Prototyping Case Study at NoVA UX A Prototyping Case Study at NoVA UX
A Prototyping Case Study at NoVA UX
 
Automated Performance Testing for Desktop Applications by Ciprian Balea
Automated Performance Testing for Desktop Applications by Ciprian BaleaAutomated Performance Testing for Desktop Applications by Ciprian Balea
Automated Performance Testing for Desktop Applications by Ciprian Balea
 
Prototyping for Business Outcomes at ModevUX
Prototyping for Business Outcomes at ModevUXPrototyping for Business Outcomes at ModevUX
Prototyping for Business Outcomes at ModevUX
 
Prototyping Your Way to Better and Faster Outcomes
Prototyping Your Way to Better and Faster Outcomes Prototyping Your Way to Better and Faster Outcomes
Prototyping Your Way to Better and Faster Outcomes
 
Cloud Platforms for Java
Cloud Platforms for JavaCloud Platforms for Java
Cloud Platforms for Java
 
MoDev East 2012 Presentation on Product Modernization
MoDev East 2012 Presentation on Product ModernizationMoDev East 2012 Presentation on Product Modernization
MoDev East 2012 Presentation on Product Modernization
 
Visualizing Relationships: Journalistic Problems in a Digital Age
Visualizing Relationships: Journalistic Problems in a Digital AgeVisualizing Relationships: Journalistic Problems in a Digital Age
Visualizing Relationships: Journalistic Problems in a Digital Age
 
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
 

Recently uploaded

Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
varshanayak241
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 

Recently uploaded (20)

Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 

Scala: Object-Oriented Meets Functional, by Iulian Dragos

  • 1. Scala: Object-Oriented meets Functional An overview of the Scala programming language Iulian Dragos 1
  • 2. What is Scala? •A programming language running on the JVM (also a JS backend) •Statically typed, combines object-orientation and functional programming •Concise •Fully interoperable with Java •As fast as Java 2
  • 3. History •“Make Java better” •1997 - Pizza and GJ (Odersky, Wadler) •2000 - Java 5 (generics) •“Make a better Java” •first release 2004 •adoption begins 2007 •first Scala conference 2008 3
  • 4. Scala drives its social graph service: 380-400 M transactions/day Migrated core messaging service from Ruby to sustain 1000x growth Approved for general production use Location-based social network. All written in Scala (>5M users). 5
  • 5. Philosophy • “Growable language” (Guy Steele) • embed DSLs/add new types • Scalable language • same concepts in small and large applications • Deep rather than broad • focus on abstraction and composition 6
  • 6. public class Person { public final String name; public final int age; Person(String name, int age) { this.name = name; this.age = age; } } class Person(val name: String, val age: Int) Java Scala 7
  • 7. import java.util.ArrayList; ... Person[] people; Person[] minors; Person[] adults; { ArrayList<Person> minorsList = new ArrayList<Person>(); ArrayList<Person> adultsList = new ArrayList<Person>(); for (int i = 0; i < people.length; i++) (people[i].age < 18 ? minorsList : adultsList) .add(people[i]); minors = minorsList.toArray(people); adults = adultsList.toArray(people); } val people: Array[Person] val (minors, adults) = people partition (_.age < 18) A tuple in a pattern match Infix method call Anonymous function 8
  • 8. Overview •Scala as an Object-Oriented Language •Scala as a Functional Programming Language •Scala as a host language for DSLs 9
  • 10. Running Scala •Compiled to Java bytecode •Read-Eval-Print-Loop (REPL) •fast turnaround •easy experimentation/testing 11
  • 11. The bottom line •Every value is an object •Everything is an expression (evaluates to a value) •Every operation is a method call •What about primitives? 12
  • 12. type can be inferred val x: Int = 10 val y = x + 10 same as x.+(10) 13
  • 13. final class Int { def +(y: Int): Int = <native> def -(y: Int): Int = <native> def *(y: Int): Int = <native> def /(y: Int): Int = <native> ... } The compiler treats all primitives as if they were instances of such classes (but uses primitive values for performance) 14
  • 14. class Complex(val re: Int, val im: Int) { def +(that: Complex) = new Complex(this.re + that.re, this.im + that.im) // .. override def toString = "%d + %di".format(re, im) } 15
  • 15. scala> val c = new Complex(1, 2) c: test.Complex = 1 + 2i scala> val c1 = new Complex(2, 2) c1: test.Complex = 2 + 2i scala> c + c1 res0: test.Complex = 3 + 4i 16
  • 16. Everything is an expression •No statements •Reduces the need for return and side-effects 17
  • 17. def max(x: Int, y: Int) = if (x > y) x else y no return statement scala> val t = { | x = x + 10 | x - 1 | } t: Int = 9 return type inferred to Int blocks evaluate to last expression 18
  • 18. What about println? scala> val x = println("hello, world") hello, world x: Unit = () The Unit type is like void in Java: no interesting value 19
  • 20. Classes •Similar to Java classes •have fields, methods, parameters and types •every member can be overridden •except classes (no virtual classes) •any member can be abstract 21
  • 21. explicit abstract class parameters/fields abstract class Node[T](val v: T, next: Node[T]) extends List(v, next) { val cache: Int def first: T def children = List(next) override def toString = "Node " + v } 22 super ctor call When compiled, this class will look and behave exactly like the obvious translation to a Java class. No glue code necessary.
  • 22. Traits •Like Java interfaces, but in addition •allow concrete methods, fields, types •Like Scala classes, but without constructor parameters •Allow (a form of) multiple inheritance •mix-in composition 23
  • 23. type parameter can extend Java classes trait Ordered[A] extends java.lang.Comparable[A] { def compare(that: A): Int def < (that: A): Boolean = (this compare that) < 0 def > (that: A): Boolean = (this compare that) > 0 def <= (that: A): Boolean = (this compare that) <= 0 def >= (that: A): Boolean = (this compare that) >= 0 } classes mixing in Ordered will get all these methods ‘for free’ class Complex extends Ordered[Complex] { def compare(that: Complex): Int = ... } 24
  • 24. if (c1 > c2) c1 else c2 val xs: List[Complex] xs.sorted 25
  • 26. Mix-in example: Cells •model mutable cells that contain a single value of some arbitrary type. •Additionally, define logging and undoable variants. 27
  • 27. A class for generic cells class Cell[T](val init: T) { private var v = init def get(): T = v def set(v1: T): Unit = { v = v1 } override def toString: String = "Cell("+ v +")" } make init available as a field mutable field 28
  • 28. A trait for undoable cells trait UndoableCell[T] extends Cell[T] { import mutable.ArrayStack private var hist = new ArrayStack[T]() override def set(v1: T): Unit = { hist.push(super.get()) super.set(v1) } def undo(): Unit = super.set(hist pop) } 29
  • 29. A trait for logging cells trait LoggingCell[T] extends Cell[T] { override def get(): T = { println("getting "+ this) super.get() } override def set(v1: T): Unit = { println("setting "+ this +" to "+ v1) super.set(v1) } } 30
  • 30. Mix-in composition new Cell(0) new Cell(0) with LoggingCell[Int] new Cell(0) with LoggingCell[Int] with UndoableCell[Int] new Cell(0) with UndoableCell[Int] with LoggingCell[Int] cell with logging cell with logging and undoing cell with undoing and logging 31
  • 31. Mix-in composition •traits can be stacked •the call to super is dynamically bound •super calls the ‘preceding’ implementation in the instantiated object 32
  • 32. Late binding for super 33 Cell myCell Undo Logging
  • 33. Modules •Scala uses objects and traits for module composition •Composition through mixins •Modules may require another module 34
  • 34. Customers and DB •Module Accounting •requires a logger, a customer service •Module Customers •requires a logger, provides the customer service 35
  • 35. trait Accounting { this: Customers => val logger: Logger customers.getCustomers // .. } trait Customers { val logger: Logger val customers: CustomerService class CustomerService { requires module Customers def getCustomers: Unit = () //... } } 36
  • 36. object App extends Accounting with Customers { val logger = // logback val customers = new CustomerService } The application uses (some) concrete implementations of Accounting and CustomerService. 37
  • 37. This gives dependency injection in the language! object TestApp extends Accounting with Customers { val logger = // println based val customers = new MockCustomerService } The test environment uses a mock CustomerService. 38
  • 38. Summary •Basic O-O features •classes and traits •type inference •mix-in composition •Advanced type system •modules and dependencies •dynamic binding of super 39
  • 40. What is FP? •Use of functions (in the mathematical sense) •referential transparency (no side-effects) •Immutable objects •Functions are values 41
  • 41. Scala as FPL •function literals and closures •use val instead of var •immutable collections in the standard library •opt-in lazy evaluation •curry-ing (definition site) 42
  • 42. Function literals scala> val succ = (x: Int) => x + 1 succ: (Int) => Int = <function1> scala> succ(1) res3: Int = 2 43
  • 43. Equivalent forms •x => x + 1 (infer type) •1 + (partial application) •_ + 1 (placeholder notation) •compare _ (eta expansion) 44
  • 44. Higher-Order Functions •functions that take or return functions •almost eliminate the need for loops over collections 45
  • 45. val xs = List(1, 2, 3, 4, 5) xs.foreach(println) scala> xs.forall(_ < 10) res5: Boolean = true scala> xs.map(_ * 2) res6: List[Int] = List(2, 4, 6, 8, 10) def findBelow(limit: Int) = persons.filter(_.age < limit) lexical scoping: functions may refer and even modify anything in scope 46
  • 46. Everything is an object •Closures are objects as well •(compiler generated) instances of trait Function1[A, B] trait Function1[R, A] { def apply(x: A): R } 47
  • 47. (More) sugar •..but why can I call succ(10)? •f(args) is desugared to f.apply(args) •you can define your own ‘apply’ methods •..can I subclass FunctionN? 48
  • 48. Library •Yes! Lots of collections are functions •Sequences are Int => T •Sets are T => Boolean •Maps are K => V 49
  • 50. Collections •Generic (List[T], Map[K, V]) •Mutable and immutable implementations (default is immutable) •Uniform (same method protocol for all collections) •Uniform Return Principle •No compiler magic! 51
  • 51. Example: Maps scala> val capitals = Map("France" -> "Paris", "Switzerland" -> "Bern", "Sweden" -> "Stockholm") capitals: immutable.Map[String,String] = Map(France -> Paris, Switzerland -> Bern, Sweden -> Stockholm) scala> capitals("France") res7: java.lang.String = Paris scala> capitals + ("Romania" -> "Bucharest") res8: immutable.Map[String,String] = Map(France -> Paris, Switzerland -> Bern, Sweden -> Stockholm, Romania -> Bucharest) scala> capitals res9: immutable.Map[String,String] = Map(France -> Paris, Switzerland -> Bern, Sweden -> Stockholm) scala> capitals.filter(_._2 == "Paris") res10: immutable.Map[String,String] = Map(France -> Paris) 52
  • 52. For-comprehensions •More general than for-loops •Used to iterate, filter and generate new collections 53
  • 53. For-comprehensions may have any number of generators p is in scope for other generators for (p <- persons; pr <- p.projects; if pr.overdue) yield p.name guard construct a new collection of the same type, element by element res10: List[String] = List(“John”, “Harry”) 54
  • 54. For-comprehensions •Desugared to calls to filter, map, flatMap •..any class implementing those methods •Could be used to query a database •implement filter, map, flatmap to generate and forward SQL •Slick, ScalaQuery, Squeryl 55
  • 56. Pattern matching •A way to deconstruct structured data •A powerful switch statement (expression, really) 57
  • 57. Pattern matching abstract class Expr case class Literal(n: Int) extends Expr case class Add(left: Expr, right: Expr) extends Expr case class Mul(left: Expr, right: Expr) extends Expr the case modifier enables these classes to participate in pattern matching 58
  • 58. Pattern matching def opt(e: Expr) = e match { case Add(l, Literal(0)) => l case Mul(l, Literal(1)) => l case _ => e } patterns can be nested literals match only their value default case 59
  • 59. Patterns everywhere •Great way to process XML, JSON, etc. •exception handlers are also patterns! •..and also declarations 60
  • 60. try { //... } catch { case EvalError(line, msg) => //.. case e: IndexOutOfBoundsException => //.. } 61
  • 62. Implicit conversions •We can “grow” the language with Complex numbers •c + 1 works, but what about 1 + c? 63
  • 63. Implicit conversions •Allow user-defined conversions •When the type does not have a member •Find a method marked implicit that takes the original type to one that has it •Call the method implicitly •It’s an error to have more than one applicable conversions 64
  • 64. implicit def intIsComplex(n: Int) = new RichInt(n) class RichInt(n: Int) { def + (other: Complex) = new Complex(n) + other } 1 + c intIsComplex(1).+(c) 65
  • 65. implicit class RichInt(n: Int) { def + (other: Complex) = new Complex(n) + other } Also a great way to adapt existing libraries 66
  • 66. implicit class RichArray[T](as: Array[T]) { def foreach(f: T => Unit) = ??? def map[U](f: T => U) = ??? def filter(f: T => Boolean) = ??? // ... } Scala arrays are Java arrays, but can still have the same API 67
  • 67. Actor-based concurrency •Threads and locks are error-prone •(in fact, mutable shared data) •Alternative: Message-passing with actors •(no sharing) •Implemented entirely in the library •(3 other implementations out there) 68
  • 68. Actors in Scala actor ! msg asynchronous message send method call receive { case Ping(a1) => a1 ! msg case Pong(_) => .. } Normal pattern match translated to PartialFunction 69
  • 70. Lots •Parallel collections library! •Staging the in •Call-by-name parameters •Specialization 71
  • 71. Where to start •Pick one of the books •Scala for the Impatient (Addison-Wesley) •Atomic Scala (Bruce Eckel) •Scala for Java refugees (blog) 72