SlideShare une entreprise Scribd logo
1  sur  41
Télécharger pour lire hors ligne
Scala
Simplifying Development
ScalaDay Italy
Milan, May 25, 2013
Mirco Dotta
Sunday, May 26, 13
Is Scala the Java of the
future?
1/38
Sunday, May 26, 13
• It has basically everything Java has now
• It has closures (planned for Java 8)
• It has rich interfaces (Java 8 defender
methods), and more
• It is completely interoperable and runs
about as fast as Java
2/38
Sunday, May 26, 13
How is Scala different
from Java?
3/38
Sunday, May 26, 13
Concise Syntax
4/38
Sunday, May 26, 13
public class Time {
private final int hours;
private final int minutes;
public Time(int hours, int minutes) {
this.hours = hours;
this.minutes = minutes;
}
public int getHours() { return hours; }
public int getMinutes() { return minutes; }
}
class Time(val hours: Int, val minutes: Int)
5/38
Sunday, May 26, 13
Statically typed
but feels dynamic
6/38
Sunday, May 26, 13
val x = 2
type is inferred
no semicolon
7/38
Sunday, May 26, 13
Unifies OOP and FP
8/38
Sunday, May 26, 13
Every value is an object
9/38
Sunday, May 26, 13
List(1,2,3).filter(x => x > 2) //> res: List[Int] = List(3)
Anonymous function Int => Boolean
List(1, 2, 3).filter(
new Function[Int, Boolean] {
def apply(x: Int): Boolean = x > 2
}
)
/
//> res: List[Int] = List(3)
Functions are “just” objects
10/38
Sunday, May 26, 13
Everything is an
expression
11/38
Sunday, May 26, 13
def max(x: Int, y: Int): Int =
if (x > y) x else y
no return statement
val x: Int = {
val y = 10
val z = 5
y + z
}
blocks evaluate to last
expression
12/38
Sunday, May 26, 13
Every operation is a
method call
13/38
Sunday, May 26, 13
val x: Int = 10
val y = x + 10
same as x.+(10)
14/38
Sunday, May 26, 13
Principles, not Rules
15/38
Sunday, May 26, 13
Users can write their own operators
16/38
Principle
Sunday, May 26, 13
class Complex(val re: Int, val im: Int = 0) {
def +(that: Complex) =
new Complex(this.re + that.re, this.im + that.im)
override def toString = s"$re + $im"
}
val c1 = new Complex(1, 2) //> c1 : Complex = 1 + 2
val c2 = new Complex(2, 2) //> c2 : Complex = 2 + 2
val c = c1 + c2 //> c : Complex = 3 + 4
default argument
17/38
Sunday, May 26, 13
• new types can look like built-in ones
• “grow the language”
Sunday, May 26, 13
Powerful collections
18/38
Sunday, May 26, 13
case class Time(hours: Int, minutes: Int = 0)
val times = List(Time(1), Time(2), Time(12,30), Time(16,15))
times.filter(time => time.hours >= 12)
//> res: List[Time] = List(Time(12,30), Time(16,15))
times.map(time => Time(time.hours + 1, time.minutes))
//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),
Time(17,15))
times.take(2) //> res: List[Time] = List(Time(1,0), Time(2,0))
times.groupBy(time => time.minutes) //> res: Map[Int,List[Time]] =
Map(30 -> List(Time(12,30)), 15 -> List(Time(16,15)), 0 ->
List(Time(1,0), Time(2,0)))
times.head //> res: Time = Time(1,0)
times.last //> res: Time = Time(16,15)
19/38
Sunday, May 26, 13
For-comprehension
20/38
Sunday, May 26, 13
• More general than for-loops
• Used to iterate, filter, and generate new
collections
21/38
Sunday, May 26, 13
for (p <- persons; pr <- p.projects;
if pr.overdue) yield p.name
may have any number of generators
guard construct a new collection of the same
type, element by element
p is in scope for other generators
22/38
Sunday, May 26, 13
times.filter(time => time.hours >= 12)
//> res: List[Time] = List(Time(12,30), Time(16,15))
for(time <- times;
if time.hours >= 12) yield time
//>res: List[Time] = List(Time(12,30), Time(16,15))
times.map(time => Time(time.hours + 1, time.minutes))
//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),
Time(17,15))
for(time <- times)
yield Time(time.hours + 1, time.minutes)
//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),
Time(17,15))
23/38
Sunday, May 26, 13
Desugared to calls to filter, map, and
flatMap
24/38
Sunday, May 26, 13
Readily available on any class
implementing those methods!
25/38
Sunday, May 26, 13
Traits
26/38
Sunday, May 26, 13
• Like Java interfaces, but traits
• can have behavior (like Java 8 interfaces
with defender methods)
• can have state
• enable multiple inheritance
27/38
Sunday, May 26, 13
public interface Comparable<T> {
int compareTo(int o);
}
trait Comparable[T] {
def compareTo(that: T): Int
}
def <(that: T): Boolean =
(this compare that) < 0
def >(that: T): Boolean =
(this compare that) > 0
//... Rich
Interface
28/38
Sunday, May 26, 13
Multiple Inheritance
• Traits can mix-in multiple traits
• Classes can mix-in multiple traits
• Both Class and Trait can inherit at most
from one Class
29/38
Sunday, May 26, 13
trait Bird {
def fly: String = "I'm flying!"
}
trait Swimmer {
def swim: String = "I'm swimming!"
}
class Fish extends Swimmer
class Duck extends Bird with Swimmer
30/38
Sunday, May 26, 13
Embedding DSLs
Sunday, May 26, 13
Scala simplifies
development because...
32/38
Sunday, May 26, 13
Less is More
33/38
Sunday, May 26, 13
Few language
constructs with high
abstraction power
34/38
Sunday, May 26, 13
It’s Fun!
35/38
Sunday, May 26, 13
Good for your
business?
36/38
Sunday, May 26, 13
37/38
Sunday, May 26, 13
Get Started in 5’
38/38
http://www.typesafe.com/platform/getstarted
Sunday, May 26, 13
Thanks
twitter: @mircodotta
Sunday, May 26, 13

Contenu connexe

Tendances

Clojure - Revenge of the Verbs
Clojure - Revenge of the VerbsClojure - Revenge of the Verbs
Clojure - Revenge of the Verbs
Tim Lossen
 
computer notes - Data Structures - 38
computer notes - Data Structures - 38computer notes - Data Structures - 38
computer notes - Data Structures - 38
ecomputernotes
 
IOEfficientParalleMatrixMultiplication_present
IOEfficientParalleMatrixMultiplication_presentIOEfficientParalleMatrixMultiplication_present
IOEfficientParalleMatrixMultiplication_present
Shubham Joshi
 

Tendances (20)

Datastage real time scenario
Datastage real time scenarioDatastage real time scenario
Datastage real time scenario
 
Intro to Matlab programming
Intro to Matlab programmingIntro to Matlab programming
Intro to Matlab programming
 
Evolution of Macros
Evolution of MacrosEvolution of Macros
Evolution of Macros
 
Enter The Matrix
Enter The MatrixEnter The Matrix
Enter The Matrix
 
Clojure - Revenge of the Verbs
Clojure - Revenge of the VerbsClojure - Revenge of the Verbs
Clojure - Revenge of the Verbs
 
R workshop xx -- Parallel Computing with R
R workshop xx -- Parallel Computing with R R workshop xx -- Parallel Computing with R
R workshop xx -- Parallel Computing with R
 
Matlab1
Matlab1Matlab1
Matlab1
 
Clojure - LISP on the JVM
Clojure - LISP on the JVM Clojure - LISP on the JVM
Clojure - LISP on the JVM
 
Procedural Content Generation with Clojure
Procedural Content Generation with ClojureProcedural Content Generation with Clojure
Procedural Content Generation with Clojure
 
Ee693 sept2014quizgt1
Ee693 sept2014quizgt1Ee693 sept2014quizgt1
Ee693 sept2014quizgt1
 
Fast, stable and scalable true radix sorting with Matt Dowle at useR! Aalborg
Fast, stable and scalable true radix sorting with Matt Dowle at useR! AalborgFast, stable and scalable true radix sorting with Matt Dowle at useR! Aalborg
Fast, stable and scalable true radix sorting with Matt Dowle at useR! Aalborg
 
computer notes - Data Structures - 38
computer notes - Data Structures - 38computer notes - Data Structures - 38
computer notes - Data Structures - 38
 
Learn basics of Clojure/script and Reagent
Learn basics of Clojure/script and ReagentLearn basics of Clojure/script and Reagent
Learn basics of Clojure/script and Reagent
 
Scala user-group-19.03.2014
Scala user-group-19.03.2014Scala user-group-19.03.2014
Scala user-group-19.03.2014
 
Ee693 sept2014quizgt2
Ee693 sept2014quizgt2Ee693 sept2014quizgt2
Ee693 sept2014quizgt2
 
Time series predictions using LSTMs
Time series predictions using LSTMsTime series predictions using LSTMs
Time series predictions using LSTMs
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
 
Clojure presentation
Clojure presentationClojure presentation
Clojure presentation
 
Computer notes - Sorting
Computer notes  - SortingComputer notes  - Sorting
Computer notes - Sorting
 
IOEfficientParalleMatrixMultiplication_present
IOEfficientParalleMatrixMultiplication_presentIOEfficientParalleMatrixMultiplication_present
IOEfficientParalleMatrixMultiplication_present
 

En vedette

Yourprezi
YourpreziYourprezi
Yourprezi
nau1234
 
Delta nu powerpoint
Delta nu powerpointDelta nu powerpoint
Delta nu powerpoint
piskadlomm
 

En vedette (10)

Scalatra - Massimiliano Dessì (Energeya)
Scalatra - Massimiliano Dessì (Energeya)Scalatra - Massimiliano Dessì (Energeya)
Scalatra - Massimiliano Dessì (Energeya)
 
Yourprezi
YourpreziYourprezi
Yourprezi
 
Delta nu powerpoint
Delta nu powerpointDelta nu powerpoint
Delta nu powerpoint
 
20 may 2013
20 may 201320 may 2013
20 may 2013
 
Andrea Lattuada, Gabriele Petronella - Building startups on Scala
Andrea Lattuada, Gabriele Petronella - Building startups on ScalaAndrea Lattuada, Gabriele Petronella - Building startups on Scala
Andrea Lattuada, Gabriele Petronella - Building startups on Scala
 
Alessandro Abbruzzetti - Kernal64
Alessandro Abbruzzetti - Kernal64Alessandro Abbruzzetti - Kernal64
Alessandro Abbruzzetti - Kernal64
 
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...
 
Alberto Paro - Hands on Scala.js
Alberto Paro - Hands on Scala.jsAlberto Paro - Hands on Scala.js
Alberto Paro - Hands on Scala.js
 
Scala Past, Present & Future
Scala Past, Present & FutureScala Past, Present & Future
Scala Past, Present & Future
 
Lightbend Lagom: Microservices Just Right
Lightbend Lagom: Microservices Just RightLightbend Lagom: Microservices Just Right
Lightbend Lagom: Microservices Just Right
 

Similaire à Simplifying development-short - Mirco Dotta (Typesafe)

Julia - Easier, Better, Faster, Stronger
Julia - Easier, Better, Faster, StrongerJulia - Easier, Better, Faster, Stronger
Julia - Easier, Better, Faster, Stronger
Kenta Sato
 
CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...
CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...
CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...
CodeFest
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
anshikagoel52
 
Intro to Functional Programming Workshop (code4lib)
Intro to Functional Programming Workshop (code4lib)Intro to Functional Programming Workshop (code4lib)
Intro to Functional Programming Workshop (code4lib)
Will Kurt
 

Similaire à Simplifying development-short - Mirco Dotta (Typesafe) (20)

Methods
MethodsMethods
Methods
 
Object - Based Programming
Object - Based ProgrammingObject - Based Programming
Object - Based Programming
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08
 
Functional Programming for Fun and Profit
Functional Programming for Fun and ProfitFunctional Programming for Fun and Profit
Functional Programming for Fun and Profit
 
IRJET- Syllabus and Timetable Generation System
IRJET- Syllabus and Timetable Generation SystemIRJET- Syllabus and Timetable Generation System
IRJET- Syllabus and Timetable Generation System
 
Julia - Easier, Better, Faster, Stronger
Julia - Easier, Better, Faster, StrongerJulia - Easier, Better, Faster, Stronger
Julia - Easier, Better, Faster, Stronger
 
CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...
CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...
CodeFest 2013. Rauschmayer A. — An overview of ECMAScript 6, the next version...
 
Whats new in ES2019
Whats new in ES2019Whats new in ES2019
Whats new in ES2019
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
python.ppt
python.pptpython.ppt
python.ppt
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
python.ppt
python.pptpython.ppt
python.ppt
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 iti
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
Intro to Functional Programming Workshop (code4lib)
Intro to Functional Programming Workshop (code4lib)Intro to Functional Programming Workshop (code4lib)
Intro to Functional Programming Workshop (code4lib)
 
Computer Science CS Project Matrix CBSE Class 12th XII .pdf
Computer Science CS Project Matrix CBSE Class 12th XII .pdfComputer Science CS Project Matrix CBSE Class 12th XII .pdf
Computer Science CS Project Matrix CBSE Class 12th XII .pdf
 

Plus de Scala Italy

Martin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaMartin Odersky - Evolution of Scala
Martin Odersky - Evolution of Scala
Scala Italy
 

Plus de Scala Italy (8)

Stefano Rocco, Roberto Bentivoglio - Scala in increasingly demanding environm...
Stefano Rocco, Roberto Bentivoglio - Scala in increasingly demanding environm...Stefano Rocco, Roberto Bentivoglio - Scala in increasingly demanding environm...
Stefano Rocco, Roberto Bentivoglio - Scala in increasingly demanding environm...
 
Federico Feroldi - Scala microservices
Federico Feroldi - Scala microservicesFederico Feroldi - Scala microservices
Federico Feroldi - Scala microservices
 
Martin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaMartin Odersky - Evolution of Scala
Martin Odersky - Evolution of Scala
 
Daniela Sfregola - Intro to Akka
Daniela Sfregola - Intro to AkkaDaniela Sfregola - Intro to Akka
Daniela Sfregola - Intro to Akka
 
Mirco Dotta - Akka Streams
Mirco Dotta - Akka StreamsMirco Dotta - Akka Streams
Mirco Dotta - Akka Streams
 
Phil Calçado - Your microservice as a function
Phil Calçado - Your microservice as a functionPhil Calçado - Your microservice as a function
Phil Calçado - Your microservice as a function
 
Scala: the language of languages - Mario Fusco (Red Hat)
Scala: the language of languages - Mario Fusco (Red Hat)Scala: the language of languages - Mario Fusco (Red Hat)
Scala: the language of languages - Mario Fusco (Red Hat)
 
Scala in pratica - Stefano Rocco (MoneyFarm)
Scala in pratica - Stefano Rocco (MoneyFarm)Scala in pratica - Stefano Rocco (MoneyFarm)
Scala in pratica - Stefano Rocco (MoneyFarm)
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 

Simplifying development-short - Mirco Dotta (Typesafe)

  • 1. Scala Simplifying Development ScalaDay Italy Milan, May 25, 2013 Mirco Dotta Sunday, May 26, 13
  • 2. Is Scala the Java of the future? 1/38 Sunday, May 26, 13
  • 3. • It has basically everything Java has now • It has closures (planned for Java 8) • It has rich interfaces (Java 8 defender methods), and more • It is completely interoperable and runs about as fast as Java 2/38 Sunday, May 26, 13
  • 4. How is Scala different from Java? 3/38 Sunday, May 26, 13
  • 6. public class Time { private final int hours; private final int minutes; public Time(int hours, int minutes) { this.hours = hours; this.minutes = minutes; } public int getHours() { return hours; } public int getMinutes() { return minutes; } } class Time(val hours: Int, val minutes: Int) 5/38 Sunday, May 26, 13
  • 7. Statically typed but feels dynamic 6/38 Sunday, May 26, 13
  • 8. val x = 2 type is inferred no semicolon 7/38 Sunday, May 26, 13
  • 9. Unifies OOP and FP 8/38 Sunday, May 26, 13
  • 10. Every value is an object 9/38 Sunday, May 26, 13
  • 11. List(1,2,3).filter(x => x > 2) //> res: List[Int] = List(3) Anonymous function Int => Boolean List(1, 2, 3).filter( new Function[Int, Boolean] { def apply(x: Int): Boolean = x > 2 } ) / //> res: List[Int] = List(3) Functions are “just” objects 10/38 Sunday, May 26, 13
  • 13. def max(x: Int, y: Int): Int = if (x > y) x else y no return statement val x: Int = { val y = 10 val z = 5 y + z } blocks evaluate to last expression 12/38 Sunday, May 26, 13
  • 14. Every operation is a method call 13/38 Sunday, May 26, 13
  • 15. val x: Int = 10 val y = x + 10 same as x.+(10) 14/38 Sunday, May 26, 13
  • 17. Users can write their own operators 16/38 Principle Sunday, May 26, 13
  • 18. class Complex(val re: Int, val im: Int = 0) { def +(that: Complex) = new Complex(this.re + that.re, this.im + that.im) override def toString = s"$re + $im" } val c1 = new Complex(1, 2) //> c1 : Complex = 1 + 2 val c2 = new Complex(2, 2) //> c2 : Complex = 2 + 2 val c = c1 + c2 //> c : Complex = 3 + 4 default argument 17/38 Sunday, May 26, 13
  • 19. • new types can look like built-in ones • “grow the language” Sunday, May 26, 13
  • 21. case class Time(hours: Int, minutes: Int = 0) val times = List(Time(1), Time(2), Time(12,30), Time(16,15)) times.filter(time => time.hours >= 12) //> res: List[Time] = List(Time(12,30), Time(16,15)) times.map(time => Time(time.hours + 1, time.minutes)) //> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30), Time(17,15)) times.take(2) //> res: List[Time] = List(Time(1,0), Time(2,0)) times.groupBy(time => time.minutes) //> res: Map[Int,List[Time]] = Map(30 -> List(Time(12,30)), 15 -> List(Time(16,15)), 0 -> List(Time(1,0), Time(2,0))) times.head //> res: Time = Time(1,0) times.last //> res: Time = Time(16,15) 19/38 Sunday, May 26, 13
  • 23. • More general than for-loops • Used to iterate, filter, and generate new collections 21/38 Sunday, May 26, 13
  • 24. for (p <- persons; pr <- p.projects; if pr.overdue) yield p.name may have any number of generators guard construct a new collection of the same type, element by element p is in scope for other generators 22/38 Sunday, May 26, 13
  • 25. times.filter(time => time.hours >= 12) //> res: List[Time] = List(Time(12,30), Time(16,15)) for(time <- times; if time.hours >= 12) yield time //>res: List[Time] = List(Time(12,30), Time(16,15)) times.map(time => Time(time.hours + 1, time.minutes)) //> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30), Time(17,15)) for(time <- times) yield Time(time.hours + 1, time.minutes) //> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30), Time(17,15)) 23/38 Sunday, May 26, 13
  • 26. Desugared to calls to filter, map, and flatMap 24/38 Sunday, May 26, 13
  • 27. Readily available on any class implementing those methods! 25/38 Sunday, May 26, 13
  • 29. • Like Java interfaces, but traits • can have behavior (like Java 8 interfaces with defender methods) • can have state • enable multiple inheritance 27/38 Sunday, May 26, 13
  • 30. public interface Comparable<T> { int compareTo(int o); } trait Comparable[T] { def compareTo(that: T): Int } def <(that: T): Boolean = (this compare that) < 0 def >(that: T): Boolean = (this compare that) > 0 //... Rich Interface 28/38 Sunday, May 26, 13
  • 31. Multiple Inheritance • Traits can mix-in multiple traits • Classes can mix-in multiple traits • Both Class and Trait can inherit at most from one Class 29/38 Sunday, May 26, 13
  • 32. trait Bird { def fly: String = "I'm flying!" } trait Swimmer { def swim: String = "I'm swimming!" } class Fish extends Swimmer class Duck extends Bird with Swimmer 30/38 Sunday, May 26, 13
  • 36. Few language constructs with high abstraction power 34/38 Sunday, May 26, 13
  • 40. Get Started in 5’ 38/38 http://www.typesafe.com/platform/getstarted Sunday, May 26, 13