Demystifying Scala

Engineering Team Lead, Sharethrough

@kelleyrobinson
Demystifying Scala
Kelley Robinson
Demystifying Scala
📍Lausanne
Engineering Team Lead, Sharethrough
@kelleyrobinson
Demystifying Scala
Kelley Robinson
Engineering Team Lead, Sharethrough
@kelleyrobinson
Kelley Robinson
DEMYSTIFYING SCALA @KELLEYROBINSON
Why Scala
How to get started
Introduction to Scala
DEMYSTIFYING SCALA @KELLEYROBINSON
Why Scala
How to get started
Introduction to Scala
Ina Garten a.k.a.
The Barefoot Contessa
DEMYSTIFYING SCALA @KELLEYROBINSON
Syntax Intro
DEMYSTIFYING SCALA @KELLEYROBINSON
“How easy is that?”.isEmpty
// Boolean = false
val chefs = List("Ina", "Martha", "Giada")
// List[String] = List(Ina, Martha, Giada)
chefs(0)
// String = Ina
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
def isStoreBoughtFine(yourName: String): Boolean = {


val areYouAnyoneButIna = yourName != "Ina Garten"



areYouAnyoneButIna
}
Many ways to write a function...
DEMYSTIFYING SCALA @KELLEYROBINSON
def isStoreBoughtFine(yourName: String): Boolean = {


yourName != "Ina Garten"
}
Many ways to write a function...
DEMYSTIFYING SCALA @KELLEYROBINSON
def isStoreBoughtFine(yourName: String): Boolean =


yourName != "Ina Garten"
Many ways to write a function...
DEMYSTIFYING SCALA @KELLEYROBINSON
def isStoreBoughtFine(yourName: String) =


yourName != "Ina Garten"
Many ways to write a function...
DEMYSTIFYING SCALA @KELLEYROBINSON
def isStoreBoughtFine(yourName: String): Boolean =


"Ina Garten”
// error: type mismatch;
// found : String
// required: Boolean
Types provide safety
DEMYSTIFYING SCALA @KELLEYROBINSON
def isStoreBoughtFine(yourName: String): Boolean
// ok
isStoreBoughtFine(“Kelley")
// oh no!
isStoreBoughtFine(42)
// error: type mismatch;
// found : Int(42)
// required: String
Calling a function
DEMYSTIFYING SCALA @KELLEYROBINSON
val barefootContessa = "Ina Garten”


val barefootContessa = "Kelley Robinson"

// <console> error: barefootContessa is already defined
val yasKween = barefootContessa.toUpperCase
println(barefootContessa)
// Ina Garten
Data is immutable by default
DEMYSTIFYING SCALA @KELLEYROBINSON
Scala in a Nutshell
Object-Oriented
Meets Functional
Have the best of both worlds.
@KELLEYROBINSONDEMYSTIFYING SCALA
DEMYSTIFYING SCALA @KELLEYROBINSON
Functional Programming
DEMYSTIFYING SCALA @KELLEYROBINSON
FP: Everything is an expression
• think in values, not side-effects
• statement is an expression with Unit return type
FP: Avoid Side Effects
Changes outside of function scope
Modifies state
Not predictable
DEMYSTIFYING SCALA @KELLEYROBINSON
FP: Referential
transparency
An expression can be replaced by
its value without changing the
program behavior
DEMYSTIFYING SCALA @KELLEYROBINSON
FP: Immutable data
Avoid modifying state
DEMYSTIFYING SCALA @KELLEYROBINSON
FP: Expressions
The programmer benefits
Understanding
Predictability
Unit testing
DEMYSTIFYING SCALA @KELLEYROBINSON
SEAMLESS JAVA INTEROP TYPE INFERENCE CONCURRENCY & DISTRIBUTION
TRAITS PATTERN MATCHING HIGHER-ORDER FUNCTIONS
Scala in a Nutshell
DEMYSTIFYING SCALA @KELLEYROBINSON
Seamless Java Interop
All the benefits of the Java
Virtual Machine (JVM)
@KELLEYROBINSONDEMYSTIFYING SCALA
Type Inference
Let the type system
work for you
DEMYSTIFYING SCALA @KELLEYROBINSON
Traits
Flexible interfaces
@KELLEYROBINSONDEMYSTIFYING SCALA
DEMYSTIFYING SCALA @KELLEYROBINSON
trait CelebrityChef



trait FoodNetworkStar extends CelebrityChef {

val showName: String

}



trait BestSellingAuthor {

val bookName: String

}



object InaGarten extends FoodNetworkStar

with BestSellingAuthor {

val bookName: String = "Cooking for Jeffrey"

val showName: String = "Barefoot Contessa"

}
DEMYSTIFYING SCALA @KELLEYROBINSON
trait CelebrityChef



trait FoodNetworkStar extends CelebrityChef

object InaGarten extends FoodNetworkStar with BestSellingAuthor
object RoyChoi extends CelebrityChef
def learnToCook(instructor: CelebrityChef) = { ... }
Pattern Matching
“Switch” on steroids
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
def matchTest(x: Int): String =
x match {

case 1 => "one"

case 2 => "two"

case _ => "many"

}
println(matchTest(3))
// “many”
Pattern Matching
DEMYSTIFYING SCALA @KELLEYROBINSON
sealed trait Ingredient

case object Produce extends Ingredient

case object Meat extends Ingredient

case object DryGood extends Ingredient



def storageLocation(i: Ingredient): String =
i match {

case Produce => "Refrigerator"

case Meat => "Freezer"

case DryGood => "Pantry"

}
Pattern Matching
Higher-order
Functions
Be expressive and succinct
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
trait Ingredient { val shelfLife: Int }
Scala
val ingredients: Array[Ingredient]

val (eatNow, eatLater) = ingredients.partition(i => i.shelfLife < 7)
Java
List<Ingredient> ingredients;

List<Ingredient> eatNow = new ArrayList<Ingredient>(ingredients.size());

List<Ingredient> eatLater = new ArrayList<Ingredient>(ingredients.size());

for (Ingredient ingredient : ingredient) {

if (ingredient.getShelfLife() < 7)

eatNow.add(ingredient);

else

eatLater.add(ingredient);

}
DEMYSTIFYING SCALA @KELLEYROBINSON
trait Ingredient { val shelfLife: Int }
Scala
val ingredients: Array[Ingredient]

val (eatNow, eatLater) = ingredients.partition(i => i.shelfLife < 7)
Partition takes a function as its argument
def partition(p: Int => Boolean)
DEMYSTIFYING SCALA @KELLEYROBINSON
These are equivalent
partition(i => i.shelfLife < 7)
partition(_.shelfLife < 7)
underscore is a convenience operator
DEMYSTIFYING SCALA @KELLEYROBINSON
Why Scala
How to get started
Introduction to Scala
DEMYSTIFYING SCALA
http://downloads.typesafe.com/website/casestudies/Sharethrough-Case-Study-v1.0.pdf
@KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
“Sharethrough developers were quickly
productive with Scala, which can be
attributed to their familiarity with Java,
Scala’s expressiveness and lack of
boilerplate code.”
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA
https://twitter.com/kelleyrobinson/status/820426791433039872
@KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
https://meta.plasm.us/posts/2015/07/11/roll-your-own-scala/
Flexibility
requires
discipline
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA
https://twitter.com/chanian/status/462103684281671681
@KELLEYROBINSON
Sad truths
about Scala
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
Scala Functional Spectrum
Java Haskell
Sad truths about Scala
DEMYSTIFYING SCALA @KELLEYROBINSON
Sad truths about Scala
DEMYSTIFYING SCALA
Sad truths about Scala
@KELLEYROBINSON
// source code yikes
object Task {

implicit val taskInstance:
Nondeterminism[Task] with Catchable[Task]
with MonadError[({type λ[α,β] =
Task[β]})#λ,Throwable] = new
Nondeterminism[Task] with Catchable[Task]
with MonadError[({type λ[α,β] =
Task[β]})#λ,Throwable] { ... }
}
DEMYSTIFYING SCALA @KELLEYROBINSON
These are equivalent
partition(i => i.shelfLife < 7)
partition(_.shelfLife < 7)
underscore is a convenience operator
DEMYSTIFYING SCALA @KELLEYROBINSON
Symbolic operators
// this is valid code
def @*%--@&#&(foo: String): String = foo
// exists for this purpose
def +(s1: String, s2: String): String = s1.concat(s2)
“Modern” Functional
Programming is
cluttered with jargon
Sad truths about Scala
DEMYSTIFYING SCALA @KELLEYROBINSON
Scala is still
pretty awesome
DEMYSTIFYING SCALA @KELLEYROBINSON
Scala is still
pretty awesome
• Powerful
• Flexible
• Creative
DEMYSTIFYING SCALA @KELLEYROBINSON
DEMYSTIFYING SCALA @KELLEYROBINSON
Why Scala
How to get started
Introduction to Scala
Scala REPL
Core Language
https://www.scala-lang.org/download/install.html
@KELLEYROBINSONDEMYSTIFYING SCALA
DEMYSTIFYING SCALA @KELLEYROBINSON
Functional
Programming
Principles in Scala
Scala Center + Coursera
https://www.coursera.org/learn/progfun1
Scala Exercises
47 Degrees
https://www.scala-exercises.org/scala_tutorial/terms_and_types
@KELLEYROBINSONDEMYSTIFYING SCALA
ScalaBridge
Introductory programming
workshops for underrepresented
groups
• Curriculum is free and open source
• Teaching is a great way to learn
https://scalabridge.gitbooks.io/curriculum/content/setup/setup.html
$
@KELLEYROBINSONDEMYSTIFYING SCALA
• Don’t be intimidated
• Have fun
• Be disciplined
@KELLEYROBINSONDEMYSTIFYING SCALA
Thank You!
@kelleyrobinson
hello@krobinson.me
1 of 65

Recommended

Forget what you think you know: Redefining functional programming for Scala by
Forget what you think you know: Redefining functional programming for ScalaForget what you think you know: Redefining functional programming for Scala
Forget what you think you know: Redefining functional programming for ScalaKelley Robinson
1.8K views78 slides
Functional Programming Essentials by
Functional Programming EssentialsFunctional Programming Essentials
Functional Programming EssentialsKelley Robinson
3.8K views70 slides
New features-in-abap-7.4 by
New features-in-abap-7.4New features-in-abap-7.4
New features-in-abap-7.4swati chavan
349 views40 slides
UI Realigning & Refactoring by Jina Bolton by
UI Realigning & Refactoring by Jina BoltonUI Realigning & Refactoring by Jina Bolton
UI Realigning & Refactoring by Jina BoltonCodemotion
2.4K views133 slides
Scala Presentation Work by
Scala Presentation WorkScala Presentation Work
Scala Presentation WorkSkills Matter
1.4K views30 slides
Ruby on Rails by
Ruby on RailsRuby on Rails
Ruby on RailsDelphiCon
349 views38 slides

More Related Content

Similar to Demystifying Scala

Selenium-online-training by
Selenium-online-trainingSelenium-online-training
Selenium-online-trainingRaghav Arora
3K views16 slides
Refactoring to Scala DSLs and LiftOff 2009 Recap by
Refactoring to Scala DSLs and LiftOff 2009 RecapRefactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 RecapDave Orme
1.2K views53 slides
A Brief, but Dense, Intro to Scala by
A Brief, but Dense, Intro to ScalaA Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaDerek Chen-Becker
1.7K views12 slides
Scala in a wild enterprise by
Scala in a wild enterpriseScala in a wild enterprise
Scala in a wild enterpriseRafael Bagmanov
1.5K views69 slides
Property-Based Testing for Services by
Property-Based Testing for ServicesProperty-Based Testing for Services
Property-Based Testing for Servicesjessitron
652 views29 slides
Scala introduction by
Scala introductionScala introduction
Scala introductionMohan Rao Pusarla
588 views29 slides

Similar to Demystifying Scala(20)

Selenium-online-training by Raghav Arora
Selenium-online-trainingSelenium-online-training
Selenium-online-training
Raghav Arora3K views
Refactoring to Scala DSLs and LiftOff 2009 Recap by Dave Orme
Refactoring to Scala DSLs and LiftOff 2009 RecapRefactoring to Scala DSLs and LiftOff 2009 Recap
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme1.2K views
Property-Based Testing for Services by jessitron
Property-Based Testing for ServicesProperty-Based Testing for Services
Property-Based Testing for Services
jessitron652 views
Why Functional Programming So Hard? by Ilya Sidorov
Why Functional Programming So Hard?Why Functional Programming So Hard?
Why Functional Programming So Hard?
Ilya Sidorov162 views
Database Refactoring With Liquibase by IASA
Database Refactoring With LiquibaseDatabase Refactoring With Liquibase
Database Refactoring With Liquibase
IASA2.1K views
Agile Database Development with Liquibase by Tim Berglund
Agile Database Development with LiquibaseAgile Database Development with Liquibase
Agile Database Development with Liquibase
Tim Berglund2.4K views
What’s New in Rails 5.0? by Unboxed
What’s New in Rails 5.0?What’s New in Rails 5.0?
What’s New in Rails 5.0?
Unboxed 1.5K views
Scala final ppt vinay by Viplav Jain
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
Viplav Jain227 views
Scala Programming Introduction by airisData
Scala Programming IntroductionScala Programming Introduction
Scala Programming Introduction
airisData1.1K views
Free The Enterprise With Ruby & Master Your Own Domain by Ken Collins
Free The Enterprise With Ruby & Master Your Own DomainFree The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own Domain
Ken Collins641 views
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD? by reactima
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD? WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima1.3K views
Building Concurrent WebObjects applications with Scala by WO Community
Building Concurrent WebObjects applications with ScalaBuilding Concurrent WebObjects applications with Scala
Building Concurrent WebObjects applications with Scala
WO Community918 views
Intro to scala by Joe Zulli
Intro to scalaIntro to scala
Intro to scala
Joe Zulli330 views

More from Kelley Robinson

Protecting your phone verification flow from fraud & abuse by
Protecting your phone verification flow from fraud & abuseProtecting your phone verification flow from fraud & abuse
Protecting your phone verification flow from fraud & abuseKelley Robinson
160 views33 slides
Auth on the web: better authentication by
Auth on the web: better authenticationAuth on the web: better authentication
Auth on the web: better authenticationKelley Robinson
138 views40 slides
WebAuthn by
WebAuthnWebAuthn
WebAuthnKelley Robinson
339 views41 slides
Introduction to Public Key Cryptography by
Introduction to Public Key CryptographyIntroduction to Public Key Cryptography
Introduction to Public Key CryptographyKelley Robinson
219 views41 slides
2FA in 2020 and Beyond by
2FA in 2020 and Beyond2FA in 2020 and Beyond
2FA in 2020 and BeyondKelley Robinson
213 views44 slides
Identiverse 2020 - Account Recovery with 2FA by
Identiverse 2020 - Account Recovery with 2FAIdentiverse 2020 - Account Recovery with 2FA
Identiverse 2020 - Account Recovery with 2FAKelley Robinson
499 views32 slides

More from Kelley Robinson(20)

Protecting your phone verification flow from fraud & abuse by Kelley Robinson
Protecting your phone verification flow from fraud & abuseProtecting your phone verification flow from fraud & abuse
Protecting your phone verification flow from fraud & abuse
Kelley Robinson160 views
Auth on the web: better authentication by Kelley Robinson
Auth on the web: better authenticationAuth on the web: better authentication
Auth on the web: better authentication
Kelley Robinson138 views
Introduction to Public Key Cryptography by Kelley Robinson
Introduction to Public Key CryptographyIntroduction to Public Key Cryptography
Introduction to Public Key Cryptography
Kelley Robinson219 views
Identiverse 2020 - Account Recovery with 2FA by Kelley Robinson
Identiverse 2020 - Account Recovery with 2FAIdentiverse 2020 - Account Recovery with 2FA
Identiverse 2020 - Account Recovery with 2FA
Kelley Robinson499 views
Designing customer account recovery in a 2FA world by Kelley Robinson
Designing customer account recovery in a 2FA worldDesigning customer account recovery in a 2FA world
Designing customer account recovery in a 2FA world
Kelley Robinson341 views
Building a Better Scala Community by Kelley Robinson
Building a Better Scala CommunityBuilding a Better Scala Community
Building a Better Scala Community
Kelley Robinson355 views
BSides SF - Contact Center Authentication by Kelley Robinson
BSides SF - Contact Center AuthenticationBSides SF - Contact Center Authentication
BSides SF - Contact Center Authentication
Kelley Robinson382 views
BSides PDX - Threat Modeling Authentication by Kelley Robinson
BSides PDX - Threat Modeling AuthenticationBSides PDX - Threat Modeling Authentication
BSides PDX - Threat Modeling Authentication
Kelley Robinson325 views

Recently uploaded

Update 42 models(Diode/General ) in SPICE PARK(DEC2023) by
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Tsuyoshi Horigome
39 views16 slides
Design of machine elements-UNIT 3.pptx by
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptxgopinathcreddy
34 views31 slides
Ansari: Practical experiences with an LLM-based Islamic Assistant by
Ansari: Practical experiences with an LLM-based Islamic AssistantAnsari: Practical experiences with an LLM-based Islamic Assistant
Ansari: Practical experiences with an LLM-based Islamic AssistantM Waleed Kadous
7 views29 slides
_MAKRIADI-FOTEINI_diploma thesis.pptx by
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptxfotinimakriadi
10 views32 slides
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf by
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdfASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdfAlhamduKure
6 views11 slides
SUMIT SQL PROJECT SUPERSTORE 1.pptx by
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptxSumit Jadhav
22 views26 slides

Recently uploaded(20)

Update 42 models(Diode/General ) in SPICE PARK(DEC2023) by Tsuyoshi Horigome
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Design of machine elements-UNIT 3.pptx by gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy34 views
Ansari: Practical experiences with an LLM-based Islamic Assistant by M Waleed Kadous
Ansari: Practical experiences with an LLM-based Islamic AssistantAnsari: Practical experiences with an LLM-based Islamic Assistant
Ansari: Practical experiences with an LLM-based Islamic Assistant
M Waleed Kadous7 views
_MAKRIADI-FOTEINI_diploma thesis.pptx by fotinimakriadi
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptx
fotinimakriadi10 views
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf by AlhamduKure
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdfASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
AlhamduKure6 views
SUMIT SQL PROJECT SUPERSTORE 1.pptx by Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 22 views
MongoDB.pdf by ArthyR3
MongoDB.pdfMongoDB.pdf
MongoDB.pdf
ArthyR349 views
Searching in Data Structure by raghavbirla63
Searching in Data StructureSearching in Data Structure
Searching in Data Structure
raghavbirla6314 views
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth by Innomantra
BCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for GrowthBCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for Growth
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth
Innomantra 10 views
GDSC Mikroskil Members Onboarding 2023.pdf by gdscmikroskil
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdf
gdscmikroskil59 views
Web Dev Session 1.pptx by VedVekhande
Web Dev Session 1.pptxWeb Dev Session 1.pptx
Web Dev Session 1.pptx
VedVekhande13 views
REACTJS.pdf by ArthyR3
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR335 views
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx by lwang78
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
lwang78165 views
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... by csegroupvn
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
csegroupvn6 views
fakenews_DBDA_Mar23.pptx by deepmitra8
fakenews_DBDA_Mar23.pptxfakenews_DBDA_Mar23.pptx
fakenews_DBDA_Mar23.pptx
deepmitra816 views

Demystifying Scala