Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to Scala
Search
Gabriele Petronella
February 18, 2015
Programming
3
180
Introduction to Scala
Milano Scala Group - Group Kickoff
Gabriele Petronella
February 18, 2015
Tweet
Share
More Decks by Gabriele Petronella
See All by Gabriele Petronella
Design System Adventures in React - ReactJS Day 2024
gabro
0
68
Design System Adventures in React
gabro
1
80
Casting Metals
gabro
0
350
Functional Programming in front-end applications
gabro
1
220
Functional Programming in Front-end Applications
gabro
3
160
How to get away with Functional Programming in front-end applications
gabro
3
1.4k
Bridging the tooling gap with Scala.js
gabro
0
260
Monad Transformers Down to Earth
gabro
2
2.5k
Move fast and fix things
gabro
0
320
Other Decks in Programming
See All in Programming
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
250
Better Code Design in PHP
afilina
0
180
The Clean ArchitectureがWebフロントエンドでしっくりこないのは何故か / Why The Clean Architecture does not fit with Web Frontend
twada
PRO
55
18k
PHPカンファレンス名古屋2025 タスク分解の試行錯誤〜レビュー負荷を下げるために〜
soichi
1
760
良いコードレビューとは
danimal141
9
8.4k
From the Wild into the Clouds - Laravel Meetup Talk
neverything
0
180
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
nobu09
2
360
Datadog DBMでなにができる? JDDUG Meetup#7
nealle
0
160
Datadog Workflow Automation で圧倒的価値提供
showwin
1
310
自力でTTSモデルを作った話
zgock999
0
120
JAWS Days 2025のインフラ
komakichi
1
350
Duke on CRaC with Jakarta EE
ivargrimstad
0
180
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
11
540
Optimising Largest Contentful Paint
csswizardry
34
3.1k
BBQ
matthewcrist
87
9.5k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
The Cult of Friendly URLs
andyhume
78
6.2k
Faster Mobile Websites
deanohume
306
31k
Code Reviewing Like a Champion
maltzj
521
39k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Making Projects Easy
brettharned
116
6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.2k
Transcript
JUST, WHAT IS Scala?
AS A START, IT'S NOT...
None
NOT that OBVIOUS, TRUST ME
HISTORY (ALMOST) 2003 - A drunken Martin Odersky sees a
Reese's Peanut Butter Cup ad featuring somebody's peanut butter getting on somebody else's chocolate and has an idea...
None
HISTORY (ALMOST) He creates Scala, a language that unifies constructs
from both object oriented and functional languages. This pisses off both groups and each promptly declares jihad. — James Iry
JOKES ASIDE...
OBJECT-ORIENTED PROGRAMMING ENCAPSULATE FUNCTIONALITIES INTO OBJECTS
RANDOM WORDS (FROM WIKIPEDIA) METHODS, MESSAGE PASSING, INFORMATION HIDING, DATA
ABSTRACTION, ENCAPSULATION, POLYMORPHISM, INHERITANCE
FUNCTIONAL PROGRAMMING ACTUAL MATHEMATICAL FUNCTIONS NO SIDE EFFECTS
RANDOM WORDS (FROM WIKIPEDIA) LAMBDA CALCULUS, COMPOSITIONALITY, RECURSION, REFERENTIAL TRANSPARENCY,
NO SIDE EFFECTS
Though OOP came from many motivations, two were central
The large scale one was to find a better module
scheme for complex systems involving hiding of details
...and the small scale one was to find a more
flexible version of assignment, and then to try to eliminate it altogether.
It is unfortunate that much of what is called “object-oriented
programming” today is simply old style programming with fancier constructs.
THE EARLY HISTORY OF SMALLTALK ALAN KAY, 1993
CAN WE HAVE OUR CAKE AND EAT IT TOO?
FUNCTIONAL + OO PROGRAMMING ???
MEET Scala
MODULAR PROGRAMMING OO FOR MODULARITY FP FOR DATA MANIPULATION
A PRACTICAL LANGUAGE
JAVA INTEROPERABILITY SCALA -> JAVA JAVA -> SCALA
EASY TO DEPLOY RUNS ON A JVM* *RAM NOT INCLUDED
FAST YEP
EXTENSIBLE BY DESIGN (MORE ON THIS LATER)
THEY TOLD ME SCALA IS COMPLICATED
None
WELL OK, MAYBE SOMETIMES...
LET'S BUILD A USER CLASS PRETTY BASIC RIGHT?
public class Person implements Serializable { private final String firstName;
private final String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
public class Person implements Serializable { private final String firstName;
private final String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Person person = (Person) o; if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) { return false; } if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) { return false; } return true; } public int hashCode() { int result = firstName != null ? firstName.hashCode() : 0; result = 31 * result + (lastName != null ? lastName.hashCode() : 0); return result; } public String toString() { return "Person(" + firstName + "," + lastName + ")"; } }
None
case class User(firstName: String, lastName: String)
None
LET'S SORT OUR USERS
JAVA Collections.sort(users, new Comparator<User>() { public int compare(User a, User
b) { return a.getLastName().compare(b.getLastName()); } });
SCALA users.sortBy(x => x.firstName) RUBY users.sort_by {|x| x.firstName}
SCALA users.sortBy(_.firstName) RUBY users.sort_by(&:firstName)
SCALABLE DESIGN FEW, BUT POWERFUL CONCEPTS
TYPES
DYNAMIC TYPING function subtract(x, y) { return x - y;
} subtract(42, 2) // 40, that was easy!
None
OR NOT...
None
DYNAMIC TYPING if (new Date().getDay() === 2) { var foo
= subtract('foo', 'fo'); return foo.concat('!'); }
QUESTION 1 IS THIS BROKEN?
BOY, IF IT IS!
QUESTION 2 WHEN WILL IT BREAK?
NEXT TUESDAY (AND THE ONE AFTER THAT, AND THE ONE
AFTER...)
QUESTION 3 WHERE WILL IT BREAK?
ONLY X KNOWS WHERE X IS YOUR FAVORITE GOD
None
LET'S TRY AGAIN
STATIC TYPING (FTW!)
None
STATIC TYPING def subtract(x: Int, y: Int): Int = x
- y subtract(42, 2) // 40, that was easy
STATIC TYPING import java.util.Date // because fuck you if (new
Date().getDay() == 2) { val foo = subtract("foo", "fo"); return foo.concat("!"); }
QUESTION 1 IS THIS BROKEN?
YOU BETCHA!
QUESTION 2 WHEN WILL IT BREAK?
RIGHT NOW ACTUALLY, AFTER IT COMPILES, SO IT MIGHT WELL
BE NEXT TUESDAY
QUESTION 3 WHERE WILL IT BREAK?
RIGHT HERE NO CELESTIAL ENTITIES INVOLVED
None
TRAITS
TRAITS trait Polite { def salutation: String def greet =
println(salutation) }
TRAITS trait Polite { def salutation: String def greet =
println(salutation) } class User(name: String) extends Polite { def salutation = s"Hello, my name is $name" }
TRAITS trait Polite { def salutation: String def greet =
println(salutation) } class User(name: String) extends Polite { def salutation = s"Hello, my name is $name" } new User("Gabriele").greet // Hello, my name is Gabriele
IMPLICIT PARAMETERS
IMPLICIT PARAMETERS val hi = "Hi there!" def greet(s: String)
= println(s) greet(hi) // Hi there!
IMPLICIT PARAMETERS implicit val hi = "Hi there!" def greet(implicit
s: String) = println(s) greet // Hi there!
IMPLICIT PARAMETERS DB.withConnection { connection => Query("SELECT * FROM USERS").execute(connection)
} DB.withConnection { implicit connection => Query("SELECT * FROM USERS").execute }
TRAITS + IMPLICITS = TYPECLASSES
TYPE CLASSES class Semigroup a where append :: a ->
a -> a instance Semigroup Integer where append a b = a + b Prelude> append 42 7 49
TYPE CLASSES trait Semigroup[A] { def append(a1: A, a2: A):
A } implicit val SemigroupInt = new Semigroup[Int] { def append(x: Int, y: Int) = x + y } def append[A](x: A, y: A)(implicit s: Semigroup[A]) = s.append(x, y) scala> append(42, 7) res0: Int = 49
IMPLICIT CONVERSIONS
IMPLICIT CONVERSIONS In ruby: 2.even? // true 2.odd? // false
IMPLICIT CONVERSIONS In scala: 2.isEven // error: value isEven is
not a member of Int
None
IMPLICIT CONVERSIONS def isEven(n: Int) = n % 2 ==
0 def isOdd(n: Int) = !isEven(n) isEven(2) // true isEven(3) // false isOdd(3) // true isOdd(2) // false
IMPLICIT CONVERSIONS class PimpedInt(n: Int) { def isEven = n
% 2 == 0 def isOdd = !isEven } new PimpedInt(2).isEven // true new PimpedInt(2).isOdd // false new PimpedInt(2) + 3 // :-(
IMPLICIT CONVERSIONS implicit def toPimpedInt(n: Int) = new PimpedInt(n) 2.isEven
// true 3.isEven // false 3.isOdd // true 2.isOdd // false like ruby, plus, it won't break next tuesday
None
SYNTAX INFIX METHODS
INFIX METHODS 1 + 2 // 3 1.+(2) // 3
List(1, 2, 3).map(x => x + 1) // List(2, 3, 4) List(1, 2, 3) map (x => x + 1) // List(2, 3, 4)
SYNTAX TRAILING PARAMETERS
TRAILING PARAMETERS def sum(x: Int)(y: Int) = x + y
sum(42)(2) // 44 sum(42) { 2 } // 44
TRAILING PARAMETERS def foo(f: Int) = f * 2 foo(32
+ 2) // 68 foo { val y = 4 * 8 y + 2 } // 68
IMPLICITS + SYNTAX = DSL
DSL lastSender ! "finished" forAll { (x: Int, y: Int)
=> (x > 0) ==> (y > 0) }
DSL (EXPLICIT VERSION) lastSender.!("finished") forAll((x: Int, y: Int) => (x
> 0).==>(y > 0))
DSL "A Stack" should "pop values in LIFO order" in
{ val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should be (2) stack.pop() should be (1) }
DSL (EXPLICIT VERSION) toShouldable("A Stack").should("pop values in LIFO order").in(x =>
val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop().should().be(2) stack.pop().should().be(1) )
SCALA RECAP
MODULAR FP + OO
PRACTICAL JAVA INTEROP, JVM, FAST
EXTENSIBLE SIMPLE BUILDING BLOCKS
THANK YOU