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
170
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
gabro
0
59
Casting Metals
gabro
0
340
Functional Programming in front-end applications
gabro
1
210
Functional Programming in Front-end Applications
gabro
3
140
How to get away with Functional Programming in front-end applications
gabro
3
1.2k
Bridging the tooling gap with Scala.js
gabro
0
240
Monad Transformers Down to Earth
gabro
2
2.2k
Move fast and fix things
gabro
0
300
Mythical trees and where to find them
gabro
0
200
Other Decks in Programming
See All in Programming
try! Swift Tokyo 2024のLT枠に採択されたプロポーザルを出すときに考えていたこと
ski
0
340
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
26
7.9k
スクラムチームと認知負荷 - ニフティのスクラムトーク Vol2. / NIFTY Tech Talk #18
niftycorp
PRO
1
120
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
250
Front-end application development, Symfony-style(s)
dunglas
2
1.9k
Ruby GitHub Packages
bkuhlmann
0
620
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
0
100
Ruby Function Composition
bkuhlmann
1
330
データアナリストが行うDatabricksを活用したETLの自動化事例
shinoa
0
260
Behind VS Code Extensions for JavaScript / TypeScript Linnting and Formatting
unvalley
4
220
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
250
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
170
Featured
See All Featured
Six Lessons from altMBA
skipperchong
20
3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
Creatively Recalculating Your Daily Design Routine
revolveconf
209
11k
Code Review Best Practice
trishagee
54
15k
What the flash - Photography Introduction
edds
64
11k
The Mythical Team-Month
searls
215
42k
GraphQLとの向き合い方2022年版
quramy
31
12k
Imperfection Machines: The Place of Print at Facebook
scottboms
258
12k
10 Git Anti Patterns You Should be Aware of
lemiorhan
646
58k
[RailsConf 2023] Rails as a piece of cake
palkan
22
3.9k
Music & Morning Musume
bryan
41
5.6k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
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