SlideShare ist ein Scribd-Unternehmen logo
1 von 54
Scala - The Simple Parts
Martin Odersky
Typesafe and EPFL
10 Years of Scala
Grown Up?
Scala’s user community is pretty large for its age group.
~ 100’000 developers
~ 200’000 subscribers to Coursera online courses.
#13 in RedMonk Language Rankings
Many successful rollouts and happy users.
But Scala is also discussed more controversially than usual
for a language at its stage of adoption.
Why?
3
Controversies
Internal controversies: Different communities don’t agree
what programming in Scala should be.
External complaints:
“Scala is too academic”
“Scala has sold out to industry”
“Scala’s types are too hard”
“Scala’s types are not strict enough”
“Scala is everything and the kitchen sink”
Signs that we have not made clear enough what the
essence of programming in Scala is.
4
1-5
The Picture So Far
Agile, with lightweight syntax
Object-Oriented Functional
Safe and performant, with strong static tpying
= scalable
What is “Scalable”?
1st meaning: “Growable”
– can be molded into new languages
by adding libraries (domain
specific or general)
See: “Growing a language”
(Guy Steele, 1998)
2nd meaning: “Enabling Growth”
– can be used for small as well as
large systems
– allows for smooth growth from
small to large.
6
A Growable Language
• Flexible Syntax
• Flexible Types
• User-definable operators
• Higher-order functions
• Implicits
...
Make it relatively easy to build new DSLs on top of Scala
And where this fails, we can always use the macro system
(even though so far it’s labeled experimental)
7
A Growable Language
8
SBT
Chisel
Spark
Spray
Dispatch
Akka
ScalaTest
Squeryl
Specs
shapeless
Scalaz
Slick
Growable = Good?
In fact, it’s a double edged sword.
– DSLs can fracture the user
community (“The Lisp curse”)
– Besides, no language is liked
by everyone, no matter whether
its a DSL or general purpose.
– Host languages get the blame
for the DSLs they embed.
Growable is great for experimentation.
But it demands conformity and discipline for large scale
production use.
9
A Language For Growth
• Can start with a one-liner.
• Can experiment quickly.
• Can grow without fearing to fall off the cliff.
• Scala deployments now go into the millions of lines of
code.
– Language works for very large programs
– Tools are challenged (build times!) but are catching up.
“A large system is one where you do not know that some of
its components even exist”
10
What Enables Growth?
Unique combination of Object/Oriented and Functional
Large systems rely on both.
Object-Oriented Functional
Unfortunately, there’s no established term for this
object/functional?
11
12
Would prefer it like this
OOPFP
But unfortunately it’s often more like this
OOP
FP

And that’s where we are 
How many OOP
people see FP
How many FP
people see OOP
Scala’s Role in History 
14
(from:James Iry: A Brief, Incomplete, and
Mostly Wrong History of Programming
Languages)
15
Another View: A Modular Language
Large Systems
Object-Oriented Functional
Small Scripts
= modular
Modular Programming
Systems should be composed from modules.
Modules should be simple parts that can be combined in
many ways to give interesting results.
– (Simple: encapsulates one functionality)
But that’s old hat!
– Should we go back to Modula-2?
– Modula-2 was limited by the Von-Neumann Bottleneck (see John
Backus’ Turing award lecture).
– Today’s systems need richer models and implementations.
16
FP is Essential for Modular Programming
Read:
“Why Functional Programming Matters”
(John Hughes, 1985).
Paraphrasing:
“Functional Programming is good because it leads to
modules that can be combined freely.”
17
Functions and Modules
Functional does not always imply Modular.
Some concepts in functional languages are at odds with
modularity:
– Haskell’s type classes
– OCaml’s records and variants
– Dynamic typing? (can argue about this one)
18
Objects and Modules
Object-oriented languages are in a sense the successors of
classical modular languages.
But Object-Oriented does not always imply Modular either.
Non-modular concepts in OOP languages:
– Smalltalk’s virtual image.
– Monkey-patching
– Mutable state makes transformations hard.
– Weak composition facilities require external DI frameworks.
– Weak decomposition facilities encourage mixing domain models
with their applications.
19
Scala – The Simple Parts
Before discussing library modules, let’s start with the
simple parts in the language itself.
They correspond directly to the fundamental actions that
together cover the essence of programming in Scala
compose – match – group – recurse – abstract – aggregate – mutate
As always: Simple ≠ Easy !
20
1. Compose
Everything is an expression
 everything can be composed with everything else.
if (age >= 18) "grownup" else "minor"
val result = tag match {
case “email” =>
try getEmail()
catch handleIOException
case “postal” =>
scanLetter()
}
21
2. Match
Pattern matching decomposes data.
It’s the dual of composition.
trait Expr
case class Number(n: Int) extends Expr
case class Plus(l: Expr, r: Expr) extends Expr
def eval(e: Expr): Int = e match {
case Number(n) => n
case Plus(l, r) => eval(l) + eval(r)
}
Simple & flexible, even if a bit verbose.
22
The traditional OO alternative
trait Expr {
def eval: Int
}
case class Number(n: Int) extends Expr {
def eval = n
}
case class Plus(l: Expr, r: Expr) extends Expr {
def eval = l.eval + r.eval
}
OK if operations are fixed and few.
But mixes data model with “business” logic.
23
3. Group
Everything can be grouped and nested.
Static scoping discipline.
Two name spaces: Terms and Types.
Same rules for each.
def solutions(target: Int): Stream[Path] = {
def isSolution(path: Path) =
path.endState.contains(target)
allPaths.filter(isSolution)
}
24
Tip: Don’t pack too much in one expression
I sometimes see stuff like this:
jp.getRawClasspath.filter(
_.getEntryKind == IClasspathEntry.CPE_SOURCE).
iterator.flatMap(entry =>
flatten(ResourcesPlugin.getWorkspace.
getRoot.findMember(entry.getPath)))
It’s amazing what you can get done in a single statement.
But that does not mean you have to do it.
25
Tip: Find meaningful names!
There’s a lot of value in meaningful names.
Easy to add them using inline vals and defs.
val sources = jp.getRawClasspath.filter(
_.getEntryKind == IClasspathEntry.CPE_SOURCE)
def workspaceRoot =
ResourcesPlugin.getWorkspace.getRoot
def filesOfEntry(entry: Set[File]) =
flatten(workspaceRoot.findMember(entry.getPath)
sources.iterator flatMap filesOfEntry
26
4. Recurse
Recursion let’s us compose to arbitrary depths.
This is almost always better than a loop.
Tail-recursive functions are guaranteed to be efficient.
@tailrec
def sameLength(xs: List[T], ys: List[U]): Boolean =
if (xs.isEmpty) ys.isEmpty
else ys.nonEmpty && sameLength(xs.tail, ys.tail)
27
5. Abstract
Functions are abstracted expressions.
Functions are themselves values.
Can be named or anonymous.
def isMinor(p: Person) = p.age < 18
val (minors, adults) = people.partition(isMinor)
val infants = minors.filter(_.age <= 3)
(this one is pretty standard by now)
(even though scope rules keep getting messed up
sometimes)
28
6. Aggregate
Collection aggregate data.
Transform instead of CRUD.
Uniform set of operations
Very simple to use
Learn one – apply everywhere
29
“The type of map is ugly / a lie”
30
Collection Objection
“The type of map is ugly / a lie”
31
Collection Objection
Why CanBuildFrom?
• Why not define it like this?
class Functor[F[_], T] {
def map[U](f: T => U): F[U]
}
• Does not work for arrays, since we need a class-tag to
build a new array.
• More generally, does not work in any case where we
need some additional information to build a new
collection.
• This is precisely what’s achieved by CanBuildFrom.
32
Collection Objection
“Set is not a Functor”
WAT? Let’s check Wikipedia:
(in fact, Set is in some sense the canonical functor!)
33
Who’s right, Mathematics or Haskell?
Crucial difference:
– In mathematics, a type comes with an equality
– In programming, a single conceptual value might have more than
one representation, so equality has to be given explicitly.
– If we construct a set, we need an equality operation to avoid
duplicates.
– In Haskell this is given by a typeclass Eq.
– But the naive functor type
def map[U](f: T => U): F[U]
does not accommodate this type class.
So the “obvious” type signature for `map` is inadequate.
Again, CanBuildFrom fixes this.
34
7. Mutate
It’s the last point on the list and should be used with
restraint.
But are vars and mutation not anti-modular?
– Indeed, global state often leads to hidden dependencies.
– But used-wisely, mutable state can cut down on boilerplate and
increase efficiency and clarity.
35
Where I use Mutable State
In dotc, a newly developed compiler for Scala:
– caching lazy vals, memoized functions,
interned names, LRU caches.
– persisting once a value is stable, store it in an object.
– copy on write avoid copying untpd.Tree to tpd.Tree.
– fresh values fresh names, unique ids
– typer state 2 vars: current constraint & current diagnostics
(versioned, explorable).
36
Why Not Use a Monad?
The fundamentalist functional approach would mandate that
typer state is represented as a monad.
Instead of now:
def typed(tree: untpd.Tree, expected: Type): tpd.Tree
def isSubType(tp1: Type, tp2: Type): Boolean
we’d write:
def typed(tree: untpd.Tree, expected: Type):
TyperState[tpd.Tree]
def isSubType(tp1: Type, tp2: Type):
TyperState[Boolean]
37
Why Not Use a Monad?
Instead of now:
if (isSubType(t1, t2) && isSubType(t2, t3)) result
we’d write:
for {
c1 <- isSubType(t1, t2)
c2 <- isSubType(t2, t3)
if c1 && c2
} yield result
Why would this be better?
38
A Question of Typing
Clojure Scala Haskell Idris Coq
syntax arguments effects values correctness
Statically checked properties
None of the 5 languages above is “right”.
It’s all a question of tradeoffs.
39
From Fundamental Actions to Simple Parts
Compose
Nest
Match
RecurseAbstract
Aggregate
Mutate
40
Expressions
Scopes
Function Values
Collections
Vars
Patterns
Functions
Modules
Modules can take a large number of forms
– A function
– An object
– A class
– An actor
– A stream transform
– A microservice
Modular programming is putting the focus on how modules
can be combined, not so much what they do.
In Scala, modules talk about values as well as types.
41
Scala’s Modular Roots
Modula-2 First language I programmed intensively
First language for which I wrote a compiler.
Modula-3 Introduced universal subtyping
Haskell Type classes  Implicit parameters
SML modules
Object ≅ Structure
Class ≅ Functor
Trait ≅ Signature
Abstract Type ≅ Abstract Type
Refinement ≅ Sharing Constraint
42
Features For Modular Programming
1. Our Vocabulary: Rich types with static checking and
functional semantics
– gives us the domains of discourse,
– gives us the means to guarantee encapsulation,
– see: “On the Criteria for Decomposing Systems into Modules”
(David Parnas, 1972).
2. Start with Objects − atomic modules
3. Parameterize with Classes − templates to create modules
dynamically
4. Mix it up with Traits − mixable slices of behavior
43
5. Abstract By Name
Members of a class or trait can be concrete or abstract.
Example: A Graph Library
44
Where To Use Abstraction?
Simple rule:
– Define what you know, leave abstract what you don’t.
– Works universally for values, methods, and types.
45
Encapsulation = Parameterization
Two sides of the coin:
1. Hide an implementation
2. Parameterize an abstraction
46
6. Abstract By Position
Parameterize classes and traits.
class List[+T]
class Set[T]
class Function1[-T, +R]
List[Number]
Set[String]
Function1[String, Int]
Variance expressed by +/- annotations
A good way to explain variance is by mapping to abstract
types.
47
Modelling Parameterized Types
class Set[T] { ... }  class Set { type $T }
Set[String]  Set { type $T = String }
class List[+T] { ... }  class List { type $T
}List[Number]  List { type $T <: Number }
Parameters  Abstract members
Arguments  Refinements
7. Keep Boilerplate Implicit
Implicit parameters are a rather simple concept
But they are surprisingly versatile!
Can represent a typeclass:
def min(x: A, b: A)(implicit cmp: Ordering[A]): A
49
Implicit Parameters
Can represent a context:
def typed(tree: untpd.Tree, expected:
Type)(implicit ctx: Context): Type
def compile(cmdLine: String)
(implicit defaultOptions: List[String]): Unit
Can represent a capability:
def accessProfile(id: CustomerId)
(implicit admin: AdminRights): Info
50
Module Parts
Rich Static
Types
Objects
Classes
Traits
Abstract
Types
Type
Parameters
Implicit
Parameters
51
parameterized
modules
atomic modules
The vocabulary
slices of behavior
abstract by name
abstract by position
cut boilerplate
Summary
A fairly modest (some might say: boring) set of parts that
can be combined in flexible ways.
Caveat: This is my selection, not everyone needs to
agree.
52
Other Parts
Here are parts that are either not as simple or do not work as
seamlessly with the core:
– Implicit conversions
– Existential types
– Structural types
– Higher-kinded types
– Macros
All of them are under language feature flags or experimental
flags.
– This makes it clear they are outside the core.
– My advice: Avoid, unless you have a clear use case.
– e.g, you use Scala as a host for a DSL or other language.
53
Thank You
Follow us on twitter:
@typesafe

Weitere ähnliche Inhalte

Was ist angesagt?

Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and objectFajar Baskoro
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Linguagem Java - Conceitos e Técnicas
Linguagem Java - Conceitos e TécnicasLinguagem Java - Conceitos e Técnicas
Linguagem Java - Conceitos e TécnicasBreno Vitorino
 
Poo programacion orientada a objetos - renee morales
Poo programacion orientada a objetos - renee moralesPoo programacion orientada a objetos - renee morales
Poo programacion orientada a objetos - renee moralesRenee Morales Calhua
 
B.sc CSIT 2nd semester C++ Unit6
B.sc CSIT  2nd semester C++ Unit6B.sc CSIT  2nd semester C++ Unit6
B.sc CSIT 2nd semester C++ Unit6Tekendra Nath Yogi
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)paramisoft
 
Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type ClassesJohn De Goes
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
10 Curso de POO en java - métodos modificadores y analizadores
10 Curso de POO en java - métodos modificadores y analizadores10 Curso de POO en java - métodos modificadores y analizadores
10 Curso de POO en java - métodos modificadores y analizadoresClara Patricia Avella Ibañez
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in javaUmamaheshwariv1
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
Basic Graphics in Java
Basic Graphics in JavaBasic Graphics in Java
Basic Graphics in JavaPrakash Kumar
 
Android OS & SDK - Getting Started
Android OS & SDK - Getting StartedAndroid OS & SDK - Getting Started
Android OS & SDK - Getting StartedHemant Chhapoliya
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#foreverredpb
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayDebasish Ghosh
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 

Was ist angesagt? (20)

Templates in c++
Templates in c++Templates in c++
Templates in c++
 
1z0-808-certification-questions-sample
1z0-808-certification-questions-sample1z0-808-certification-questions-sample
1z0-808-certification-questions-sample
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Linguagem Java - Conceitos e Técnicas
Linguagem Java - Conceitos e TécnicasLinguagem Java - Conceitos e Técnicas
Linguagem Java - Conceitos e Técnicas
 
Poo programacion orientada a objetos - renee morales
Poo programacion orientada a objetos - renee moralesPoo programacion orientada a objetos - renee morales
Poo programacion orientada a objetos - renee morales
 
B.sc CSIT 2nd semester C++ Unit6
B.sc CSIT  2nd semester C++ Unit6B.sc CSIT  2nd semester C++ Unit6
B.sc CSIT 2nd semester C++ Unit6
 
Java String
Java String Java String
Java String
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type Classes
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
10 Curso de POO en java - métodos modificadores y analizadores
10 Curso de POO en java - métodos modificadores y analizadores10 Curso de POO en java - métodos modificadores y analizadores
10 Curso de POO en java - métodos modificadores y analizadores
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Basic Graphics in Java
Basic Graphics in JavaBasic Graphics in Java
Basic Graphics in Java
 
Android OS & SDK - Getting Started
Android OS & SDK - Getting StartedAndroid OS & SDK - Getting Started
Android OS & SDK - Getting Started
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Functional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 WayFunctional Domain Modeling - The ZIO 2 Way
Functional Domain Modeling - The ZIO 2 Way
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 

Andere mochten auch

A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to ScalaTim Underwood
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
Scala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyScala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyBozhidar Bozhanov
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in ScalaPatrick Nicolas
 
Why Scala Is Taking Over the Big Data World
Why Scala Is Taking Over the Big Data WorldWhy Scala Is Taking Over the Big Data World
Why Scala Is Taking Over the Big Data WorldDean Wampler
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to ScalaRahul Jain
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San FranciscoMartin Odersky
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming FundamentalsShahriar Hyder
 
Scala the good and bad parts
Scala the good and bad partsScala the good and bad parts
Scala the good and bad partsbenewu
 
Compilers Are Databases
Compilers Are DatabasesCompilers Are Databases
Compilers Are DatabasesMartin Odersky
 
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 Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of ScalaMartin Odersky
 
Zero to Streaming: Spark and Cassandra
Zero to Streaming: Spark and CassandraZero to Streaming: Spark and Cassandra
Zero to Streaming: Spark and CassandraRussell Spitzer
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 

Andere mochten auch (20)

A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
Why Scala?
Why Scala?Why Scala?
Why Scala?
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Scala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyScala - the good, the bad and the very ugly
Scala - the good, the bad and the very ugly
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Scala
ScalaScala
Scala
 
Why Scala Is Taking Over the Big Data World
Why Scala Is Taking Over the Big Data WorldWhy Scala Is Taking Over the Big Data World
Why Scala Is Taking Over the Big Data World
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San Francisco
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
 
Scala the good and bad parts
Scala the good and bad partsScala the good and bad parts
Scala the good and bad parts
 
Compilers Are Databases
Compilers Are DatabasesCompilers Are Databases
Compilers Are Databases
 
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 Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Scalax
ScalaxScalax
Scalax
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of Scala
 
Zero to Streaming: Spark and Cassandra
Zero to Streaming: Spark and CassandraZero to Streaming: Spark and Cassandra
Zero to Streaming: Spark and Cassandra
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 

Ähnlich wie Scala - The Simple Parts, SFScala presentation

flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slidesMartin Odersky
 
Martin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaMartin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaScala Italy
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
About Functional Programming
About Functional ProgrammingAbout Functional Programming
About Functional ProgrammingAapo Kyrölä
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinayViplav Jain
 
The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論scalaconfjp
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistpmanvi
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMarakana Inc.
 
Scala for n00bs by a n00b.
Scala for n00bs by a n00b.Scala for n00bs by a n00b.
Scala for n00bs by a n00b.brandongulla
 
Principled io in_scala_2019_distribution
Principled io in_scala_2019_distributionPrincipled io in_scala_2019_distribution
Principled io in_scala_2019_distributionRaymond Tay
 
Beginning scala 02 15
Beginning scala 02 15Beginning scala 02 15
Beginning scala 02 15lancegatlin
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesDominic Graefen
 
Indic threads pune12-polyglot & functional programming on jvm
Indic threads pune12-polyglot & functional programming on jvmIndic threads pune12-polyglot & functional programming on jvm
Indic threads pune12-polyglot & functional programming on jvmIndicThreads
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture OneAngelo Corsaro
 

Ähnlich wie Scala - The Simple Parts, SFScala presentation (20)

flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slides
 
Flatmap
FlatmapFlatmap
Flatmap
 
Martin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaMartin Odersky - Evolution of Scala
Martin Odersky - Evolution of Scala
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Devoxx
DevoxxDevoxx
Devoxx
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
About Functional Programming
About Functional ProgrammingAbout Functional Programming
About Functional Programming
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論The Evolution of Scala / Scala進化論
The Evolution of Scala / Scala進化論
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologist
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for Scala
 
Scala for n00bs by a n00b.
Scala for n00bs by a n00b.Scala for n00bs by a n00b.
Scala for n00bs by a n00b.
 
Principled io in_scala_2019_distribution
Principled io in_scala_2019_distributionPrincipled io in_scala_2019_distribution
Principled io in_scala_2019_distribution
 
Beginning scala 02 15
Beginning scala 02 15Beginning scala 02 15
Beginning scala 02 15
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love Parantheses
 
Indic threads pune12-polyglot & functional programming on jvm
Indic threads pune12-polyglot & functional programming on jvmIndic threads pune12-polyglot & functional programming on jvm
Indic threads pune12-polyglot & functional programming on jvm
 
Programming in Scala - Lecture One
Programming in Scala - Lecture OneProgramming in Scala - Lecture One
Programming in Scala - Lecture One
 

Mehr von Martin Odersky

Capabilities for Resources and Effects
Capabilities for Resources and EffectsCapabilities for Resources and Effects
Capabilities for Resources and EffectsMartin Odersky
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin Odersky
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin Odersky
 
Implementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyImplementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyMartin Odersky
 
Oscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleOscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleMartin Odersky
 
Scala eXchange opening
Scala eXchange openingScala eXchange opening
Scala eXchange openingMartin Odersky
 

Mehr von Martin Odersky (9)

scalar.pdf
scalar.pdfscalar.pdf
scalar.pdf
 
Capabilities for Resources and Effects
Capabilities for Resources and EffectsCapabilities for Resources and Effects
Capabilities for Resources and Effects
 
Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Simplicitly
SimplicitlySimplicitly
Simplicitly
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
Implementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in DottyImplementing Higher-Kinded Types in Dotty
Implementing Higher-Kinded Types in Dotty
 
Oscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simpleOscon keynote: Working hard to keep it simple
Oscon keynote: Working hard to keep it simple
 
Scala eXchange opening
Scala eXchange openingScala eXchange opening
Scala eXchange opening
 

Kürzlich hochgeladen

A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1KnowledgeSeed
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionWave PLM
 
SQL Injection Introduction and Prevention
SQL Injection Introduction and PreventionSQL Injection Introduction and Prevention
SQL Injection Introduction and PreventionMohammed Fazuluddin
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfsteffenkarlsson2
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfFurqanuddin10
 
Naer Toolbar Redesign - Usability Research Synthesis
Naer Toolbar Redesign - Usability Research SynthesisNaer Toolbar Redesign - Usability Research Synthesis
Naer Toolbar Redesign - Usability Research Synthesisparimabajra
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfkalichargn70th171
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...rajkumar669520
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAlluxio, Inc.
 
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdfMicrosoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdfQ-Advise
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Henry Schreiner
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAShane Coughlan
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Andrea Goulet
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationHelp Desk Migration
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...OnePlan Solutions
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMarkus Moeller
 
how-to-download-files-safely-from-the-internet.pdf
how-to-download-files-safely-from-the-internet.pdfhow-to-download-files-safely-from-the-internet.pdf
how-to-download-files-safely-from-the-internet.pdfMehmet Akar
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAlluxio, Inc.
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesNeo4j
 

Kürzlich hochgeladen (20)

A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion Production
 
SQL Injection Introduction and Prevention
SQL Injection Introduction and PreventionSQL Injection Introduction and Prevention
SQL Injection Introduction and Prevention
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdf
 
Naer Toolbar Redesign - Usability Research Synthesis
Naer Toolbar Redesign - Usability Research SynthesisNaer Toolbar Redesign - Usability Research Synthesis
Naer Toolbar Redesign - Usability Research Synthesis
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdfMicrosoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data Migration
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
how-to-download-files-safely-from-the-internet.pdf
how-to-download-files-safely-from-the-internet.pdfhow-to-download-files-safely-from-the-internet.pdf
how-to-download-files-safely-from-the-internet.pdf
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
 

Scala - The Simple Parts, SFScala presentation

  • 1. Scala - The Simple Parts Martin Odersky Typesafe and EPFL
  • 2. 10 Years of Scala
  • 3. Grown Up? Scala’s user community is pretty large for its age group. ~ 100’000 developers ~ 200’000 subscribers to Coursera online courses. #13 in RedMonk Language Rankings Many successful rollouts and happy users. But Scala is also discussed more controversially than usual for a language at its stage of adoption. Why? 3
  • 4. Controversies Internal controversies: Different communities don’t agree what programming in Scala should be. External complaints: “Scala is too academic” “Scala has sold out to industry” “Scala’s types are too hard” “Scala’s types are not strict enough” “Scala is everything and the kitchen sink” Signs that we have not made clear enough what the essence of programming in Scala is. 4
  • 5. 1-5 The Picture So Far Agile, with lightweight syntax Object-Oriented Functional Safe and performant, with strong static tpying = scalable
  • 6. What is “Scalable”? 1st meaning: “Growable” – can be molded into new languages by adding libraries (domain specific or general) See: “Growing a language” (Guy Steele, 1998) 2nd meaning: “Enabling Growth” – can be used for small as well as large systems – allows for smooth growth from small to large. 6
  • 7. A Growable Language • Flexible Syntax • Flexible Types • User-definable operators • Higher-order functions • Implicits ... Make it relatively easy to build new DSLs on top of Scala And where this fails, we can always use the macro system (even though so far it’s labeled experimental) 7
  • 9. Growable = Good? In fact, it’s a double edged sword. – DSLs can fracture the user community (“The Lisp curse”) – Besides, no language is liked by everyone, no matter whether its a DSL or general purpose. – Host languages get the blame for the DSLs they embed. Growable is great for experimentation. But it demands conformity and discipline for large scale production use. 9
  • 10. A Language For Growth • Can start with a one-liner. • Can experiment quickly. • Can grow without fearing to fall off the cliff. • Scala deployments now go into the millions of lines of code. – Language works for very large programs – Tools are challenged (build times!) but are catching up. “A large system is one where you do not know that some of its components even exist” 10
  • 11. What Enables Growth? Unique combination of Object/Oriented and Functional Large systems rely on both. Object-Oriented Functional Unfortunately, there’s no established term for this object/functional? 11
  • 12. 12 Would prefer it like this OOPFP
  • 13. But unfortunately it’s often more like this OOP FP  And that’s where we are  How many OOP people see FP How many FP people see OOP
  • 14. Scala’s Role in History  14 (from:James Iry: A Brief, Incomplete, and Mostly Wrong History of Programming Languages)
  • 15. 15 Another View: A Modular Language Large Systems Object-Oriented Functional Small Scripts = modular
  • 16. Modular Programming Systems should be composed from modules. Modules should be simple parts that can be combined in many ways to give interesting results. – (Simple: encapsulates one functionality) But that’s old hat! – Should we go back to Modula-2? – Modula-2 was limited by the Von-Neumann Bottleneck (see John Backus’ Turing award lecture). – Today’s systems need richer models and implementations. 16
  • 17. FP is Essential for Modular Programming Read: “Why Functional Programming Matters” (John Hughes, 1985). Paraphrasing: “Functional Programming is good because it leads to modules that can be combined freely.” 17
  • 18. Functions and Modules Functional does not always imply Modular. Some concepts in functional languages are at odds with modularity: – Haskell’s type classes – OCaml’s records and variants – Dynamic typing? (can argue about this one) 18
  • 19. Objects and Modules Object-oriented languages are in a sense the successors of classical modular languages. But Object-Oriented does not always imply Modular either. Non-modular concepts in OOP languages: – Smalltalk’s virtual image. – Monkey-patching – Mutable state makes transformations hard. – Weak composition facilities require external DI frameworks. – Weak decomposition facilities encourage mixing domain models with their applications. 19
  • 20. Scala – The Simple Parts Before discussing library modules, let’s start with the simple parts in the language itself. They correspond directly to the fundamental actions that together cover the essence of programming in Scala compose – match – group – recurse – abstract – aggregate – mutate As always: Simple ≠ Easy ! 20
  • 21. 1. Compose Everything is an expression  everything can be composed with everything else. if (age >= 18) "grownup" else "minor" val result = tag match { case “email” => try getEmail() catch handleIOException case “postal” => scanLetter() } 21
  • 22. 2. Match Pattern matching decomposes data. It’s the dual of composition. trait Expr case class Number(n: Int) extends Expr case class Plus(l: Expr, r: Expr) extends Expr def eval(e: Expr): Int = e match { case Number(n) => n case Plus(l, r) => eval(l) + eval(r) } Simple & flexible, even if a bit verbose. 22
  • 23. The traditional OO alternative trait Expr { def eval: Int } case class Number(n: Int) extends Expr { def eval = n } case class Plus(l: Expr, r: Expr) extends Expr { def eval = l.eval + r.eval } OK if operations are fixed and few. But mixes data model with “business” logic. 23
  • 24. 3. Group Everything can be grouped and nested. Static scoping discipline. Two name spaces: Terms and Types. Same rules for each. def solutions(target: Int): Stream[Path] = { def isSolution(path: Path) = path.endState.contains(target) allPaths.filter(isSolution) } 24
  • 25. Tip: Don’t pack too much in one expression I sometimes see stuff like this: jp.getRawClasspath.filter( _.getEntryKind == IClasspathEntry.CPE_SOURCE). iterator.flatMap(entry => flatten(ResourcesPlugin.getWorkspace. getRoot.findMember(entry.getPath))) It’s amazing what you can get done in a single statement. But that does not mean you have to do it. 25
  • 26. Tip: Find meaningful names! There’s a lot of value in meaningful names. Easy to add them using inline vals and defs. val sources = jp.getRawClasspath.filter( _.getEntryKind == IClasspathEntry.CPE_SOURCE) def workspaceRoot = ResourcesPlugin.getWorkspace.getRoot def filesOfEntry(entry: Set[File]) = flatten(workspaceRoot.findMember(entry.getPath) sources.iterator flatMap filesOfEntry 26
  • 27. 4. Recurse Recursion let’s us compose to arbitrary depths. This is almost always better than a loop. Tail-recursive functions are guaranteed to be efficient. @tailrec def sameLength(xs: List[T], ys: List[U]): Boolean = if (xs.isEmpty) ys.isEmpty else ys.nonEmpty && sameLength(xs.tail, ys.tail) 27
  • 28. 5. Abstract Functions are abstracted expressions. Functions are themselves values. Can be named or anonymous. def isMinor(p: Person) = p.age < 18 val (minors, adults) = people.partition(isMinor) val infants = minors.filter(_.age <= 3) (this one is pretty standard by now) (even though scope rules keep getting messed up sometimes) 28
  • 29. 6. Aggregate Collection aggregate data. Transform instead of CRUD. Uniform set of operations Very simple to use Learn one – apply everywhere 29
  • 30. “The type of map is ugly / a lie” 30 Collection Objection
  • 31. “The type of map is ugly / a lie” 31 Collection Objection
  • 32. Why CanBuildFrom? • Why not define it like this? class Functor[F[_], T] { def map[U](f: T => U): F[U] } • Does not work for arrays, since we need a class-tag to build a new array. • More generally, does not work in any case where we need some additional information to build a new collection. • This is precisely what’s achieved by CanBuildFrom. 32
  • 33. Collection Objection “Set is not a Functor” WAT? Let’s check Wikipedia: (in fact, Set is in some sense the canonical functor!) 33
  • 34. Who’s right, Mathematics or Haskell? Crucial difference: – In mathematics, a type comes with an equality – In programming, a single conceptual value might have more than one representation, so equality has to be given explicitly. – If we construct a set, we need an equality operation to avoid duplicates. – In Haskell this is given by a typeclass Eq. – But the naive functor type def map[U](f: T => U): F[U] does not accommodate this type class. So the “obvious” type signature for `map` is inadequate. Again, CanBuildFrom fixes this. 34
  • 35. 7. Mutate It’s the last point on the list and should be used with restraint. But are vars and mutation not anti-modular? – Indeed, global state often leads to hidden dependencies. – But used-wisely, mutable state can cut down on boilerplate and increase efficiency and clarity. 35
  • 36. Where I use Mutable State In dotc, a newly developed compiler for Scala: – caching lazy vals, memoized functions, interned names, LRU caches. – persisting once a value is stable, store it in an object. – copy on write avoid copying untpd.Tree to tpd.Tree. – fresh values fresh names, unique ids – typer state 2 vars: current constraint & current diagnostics (versioned, explorable). 36
  • 37. Why Not Use a Monad? The fundamentalist functional approach would mandate that typer state is represented as a monad. Instead of now: def typed(tree: untpd.Tree, expected: Type): tpd.Tree def isSubType(tp1: Type, tp2: Type): Boolean we’d write: def typed(tree: untpd.Tree, expected: Type): TyperState[tpd.Tree] def isSubType(tp1: Type, tp2: Type): TyperState[Boolean] 37
  • 38. Why Not Use a Monad? Instead of now: if (isSubType(t1, t2) && isSubType(t2, t3)) result we’d write: for { c1 <- isSubType(t1, t2) c2 <- isSubType(t2, t3) if c1 && c2 } yield result Why would this be better? 38
  • 39. A Question of Typing Clojure Scala Haskell Idris Coq syntax arguments effects values correctness Statically checked properties None of the 5 languages above is “right”. It’s all a question of tradeoffs. 39
  • 40. From Fundamental Actions to Simple Parts Compose Nest Match RecurseAbstract Aggregate Mutate 40 Expressions Scopes Function Values Collections Vars Patterns Functions
  • 41. Modules Modules can take a large number of forms – A function – An object – A class – An actor – A stream transform – A microservice Modular programming is putting the focus on how modules can be combined, not so much what they do. In Scala, modules talk about values as well as types. 41
  • 42. Scala’s Modular Roots Modula-2 First language I programmed intensively First language for which I wrote a compiler. Modula-3 Introduced universal subtyping Haskell Type classes  Implicit parameters SML modules Object ≅ Structure Class ≅ Functor Trait ≅ Signature Abstract Type ≅ Abstract Type Refinement ≅ Sharing Constraint 42
  • 43. Features For Modular Programming 1. Our Vocabulary: Rich types with static checking and functional semantics – gives us the domains of discourse, – gives us the means to guarantee encapsulation, – see: “On the Criteria for Decomposing Systems into Modules” (David Parnas, 1972). 2. Start with Objects − atomic modules 3. Parameterize with Classes − templates to create modules dynamically 4. Mix it up with Traits − mixable slices of behavior 43
  • 44. 5. Abstract By Name Members of a class or trait can be concrete or abstract. Example: A Graph Library 44
  • 45. Where To Use Abstraction? Simple rule: – Define what you know, leave abstract what you don’t. – Works universally for values, methods, and types. 45
  • 46. Encapsulation = Parameterization Two sides of the coin: 1. Hide an implementation 2. Parameterize an abstraction 46
  • 47. 6. Abstract By Position Parameterize classes and traits. class List[+T] class Set[T] class Function1[-T, +R] List[Number] Set[String] Function1[String, Int] Variance expressed by +/- annotations A good way to explain variance is by mapping to abstract types. 47
  • 48. Modelling Parameterized Types class Set[T] { ... }  class Set { type $T } Set[String]  Set { type $T = String } class List[+T] { ... }  class List { type $T }List[Number]  List { type $T <: Number } Parameters  Abstract members Arguments  Refinements
  • 49. 7. Keep Boilerplate Implicit Implicit parameters are a rather simple concept But they are surprisingly versatile! Can represent a typeclass: def min(x: A, b: A)(implicit cmp: Ordering[A]): A 49
  • 50. Implicit Parameters Can represent a context: def typed(tree: untpd.Tree, expected: Type)(implicit ctx: Context): Type def compile(cmdLine: String) (implicit defaultOptions: List[String]): Unit Can represent a capability: def accessProfile(id: CustomerId) (implicit admin: AdminRights): Info 50
  • 51. Module Parts Rich Static Types Objects Classes Traits Abstract Types Type Parameters Implicit Parameters 51 parameterized modules atomic modules The vocabulary slices of behavior abstract by name abstract by position cut boilerplate
  • 52. Summary A fairly modest (some might say: boring) set of parts that can be combined in flexible ways. Caveat: This is my selection, not everyone needs to agree. 52
  • 53. Other Parts Here are parts that are either not as simple or do not work as seamlessly with the core: – Implicit conversions – Existential types – Structural types – Higher-kinded types – Macros All of them are under language feature flags or experimental flags. – This makes it clear they are outside the core. – My advice: Avoid, unless you have a clear use case. – e.g, you use Scala as a host for a DSL or other language. 53
  • 54. Thank You Follow us on twitter: @typesafe