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
67
Design System Adventures in React
gabro
1
78
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
コミュニティ駆動 AWS CDK ライブラリ「Open Constructs Library」 / community-cdk-library
gotok365
2
240
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
830
Better Code Design in PHP
afilina
0
170
Datadog Workflow Automation で圧倒的価値提供
showwin
1
160
.NET Frameworkでも汎用ホストが使いたい!
tomokusaba
0
200
負債になりにくいCSSをデザイナとつくるには?
fsubal
10
2.6k
Ça bouge du côté des animations CSS !
goetter
2
150
機能が複雑化しても 頼りになる FactoryBotの話
tamikof
0
150
Datadog DBMでなにができる? JDDUG Meetup#7
nealle
0
140
ML.NETで始める機械学習
ymd65536
0
230
バッチを作らなきゃとなったときに考えること
irof
2
530
Bedrock Agentsレスポンス解析によるAgentのOps
licux
3
920
Featured
See All Featured
Visualization
eitanlees
146
15k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
990
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
GraphQLとの向き合い方2022年版
quramy
44
14k
Building Applications with DynamoDB
mza
93
6.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
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