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
なぜイベント駆動が必要なのか - CQRS/ESで解く複雑系システムの課題 -
j5ik2o
14
4.8k
Djangoにおける複数ユーザー種別認証の設計アプローチ@DjangoCongress JP 2025
delhi09
PRO
4
500
DRFを少しずつ オニオンアーキテクチャに寄せていく DjangoCongress JP 2025
nealle
2
290
もう僕は OpenAPI を書きたくない
sgash708
6
1.9k
iOSでQRコード生成奮闘記
ktcryomm
2
120
LINE messaging APIを使ってGoogleカレンダーと連携した予約ツールを作ってみた
takumakoike
0
130
複数のAWSアカウントから横断で 利用する Lambda Authorizer の作り方
tc3jp
0
120
color-scheme: light dark; を完全に理解する
uhyo
7
500
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
260
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
9
2.6k
クリーンアーキテクチャから見る依存の向きの大切さ
shimabox
5
1.1k
やっと腹落ち「スプリント毎に動くモノをリリースする」〜ゼロから始めるメガバンクグループのアジャイル実践〜
sasakendayo
0
120
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
47
7.4k
What's in a price? How to price your products and services
michaelherold
244
12k
The World Runs on Bad Software
bkeepers
PRO
67
11k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
175
52k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.4k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Done Done
chrislema
182
16k
Designing for Performance
lara
605
68k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
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