Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
58
Design System Adventures in React
gabro
1
71
Casting Metals
gabro
0
340
Functional Programming in front-end applications
gabro
1
220
Functional Programming in Front-end Applications
gabro
3
150
How to get away with Functional Programming in front-end applications
gabro
3
1.3k
Bridging the tooling gap with Scala.js
gabro
0
260
Monad Transformers Down to Earth
gabro
2
2.4k
Move fast and fix things
gabro
0
310
Other Decks in Programming
See All in Programming
.NET 9アプリをCGIとして レンタルサーバーで動かす
mayuki
0
720
layerx_20241129.pdf
kyoheig3
1
180
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
580
subpath importsで始めるモック生活
10tera
0
380
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
410
イマのCSSでできる インタラクション最前線 + CSS最新情報
clockmaker
5
3.7k
React CompilerとFine Grained Reactivityと宣言的UIのこれから / The next chapter of declarative UI
ssssota
7
2.7k
macOS なしで iOS アプリを開発する(※ただし xxx に限る)
mitsuharu
1
150
romajip: 日本の住所CSVデータを活用した英語住所変換ライブラリを作った話
sangunkang
0
2.2k
AWS AppSyncを用いた GraphQL APIの開発について - NIFTY Tech Talk #22
niftycorp
PRO
0
110
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
140
5分ぐらいで分かる、トリミング機能の作り方
tsutsuitakumi
0
140
Featured
See All Featured
Making Projects Easy
brettharned
115
5.9k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Side Projects
sachag
452
42k
Why Our Code Smells
bkeepers
PRO
334
57k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Scaling GitHub
holman
458
140k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
24k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Agile that works and the tools we love
rasmusluckow
327
21k
The Cost Of JavaScript in 2023
addyosmani
45
6.9k
A designer walks into a library…
pauljervisheath
204
24k
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