SlideShare una empresa de Scribd logo
1 de 41
Descargar para leer sin conexión
SNAKES & LADDERS:
A TASTE OF SCALA FOR
PYTHONISTAS
1
SCALA(EPFL, LAUSANNE):
FUNCTIONAL, OO, ADVANCED TYPE
SYSTEM
2
PYTHON(CWI AMSTERDAM):
MULTI-PARADIGM
3
CAN WE GO FROM ONE TO
THE OTHER?
WHY SHOULD WE?
4
SCALA IS THE LANGUAGE SHOWING
FASTEST GROWTH IN THE DATA SCIENCE
AND ENGINEERING FIELD1
1
The Most Popular Language For Machine Learning and Data Science Is …
5
SOME REASONS WHY SCALA IS TRENDY
1. Runs in the JVM (interoperates with Java !)
2. Apache Spark is in Scala (can be used from Python ")
3. Scala developers earn more #
6
BUT...
1. It runs on the JVM... Yikes! !
2. Scala looks weird "
3. There is a lot of jargon in Scala #
7
SO...
TODAY WE'LL SEE HOW SCALA IS NOT
THAT WEIRD IF YOU KNOW (WEIRD)
PYTHON
8
WHOAMI
> Ruben Berenguel
> PhD in Mathematics
> (big) data consultant
> Writing some Python for the past 12 years
> Started with Scala a year ago
> Right now at Affectv in London (we are hiring!)
9
SCALA IS STATICALLY
TYPED: NO int + str IN
PRODUCTION
10
WE HAVE MYPY: PYTHON'S
STATIC TYPE CHECKER!
11
# mypy_example.py:
from typing import List
answer = 42 # type: int
dont = "panic" # type: str
again = ["don't", "panic"] # type: List[str]
Typecheck the file with: mypy mypy_example.py: !
12
# mypy_bad.py
from typing import List
answer = 42 # type: str
dont = "panic" # type: int
again = ["don't", "panic"] # type: List[float]
Typecheck the file with: mypy mypy_example.py: !
mypy_bad.py:3: error: Incompatible types in assignment
(expression has type "int", variable has type "str")
mypy_bad.py:5: error: Incompatible types in assignment
(expression has type "str", variable has type "int")
mypy_bad.py:7: error: List item 0 has incompatible type "str"
mypy_bad.py:7: error: List item 1 has incompatible type "str"
13
SCALA LETS
YOU DO FANCY
TYPE-LEVEL
STUFF, BUT
THE BASICS
ARE THE SAME
AS IN MYPY14
scala> val answer: Int = 42
answer: Int = 42
scala> val dont: String = "panic"
dont: String = panic
scala> val again: List[String] = List("don't", "panic")
again: List[String] = List(don't, panic)
scala> val noAnswer: Int = "panic"
<console>:11: error: type mismatch;
found : String("panic")
required: Int
val noAnswers: Int = "panic"
15
CAN ALSO INFER TYPES
scala> val answer = 42
answer: Int = 42
scala> val dont = "panic"
dont: String = panic
scala> val again = List("don't", "panic")
again: List[String] = List(don't, panic)
16
TYPE INFERENCE IS DONE
BY THE TYPERtyper 4 THE MEAT AND POTATOES: TYPE THE TREES
17
THE SCALA
COMPILER HAS
24 PHASES
(SLOOOW)
18
CASE
CLASSES ARE
COOL
19
scala> case class Person(name: String, age: Int) {
| def greet = s"Hello, $name"
| }
defined class Person
scala> val someone = Person("Joe Doe", 42)
someone: Person = Person(Joe Doe,42)
Q: is greet a function?
A: In Scala, everything is (kind of) a function2
2
Everything is an expression (hence has a value), and that value is actually an object !
20
BECAUSE OF PATTERN
MATCHING
21
// someone: Person = Person(Joe Doe,42)
scala> someone match {
| case Person("Joe Doe", _) => someone.greet
| case _ => "Who are you?"
| }
//res0: String = Hello, Joe Doe
// someoneElse: Person = Person(Jane Doe,43)
scala> someoneElse match {
| case Person("Joe Doe", _) => someoneElse.greet
| case _ => "Who are you?"
| }
//res1: String = Who are you?
22
SADLY WE DON'T HAVE
ANYTHING SIMILAR IN
PYTHON(BUT WE LAUGH LAST: WHEN SCALA DEVS NEED TO MOCK OBJECTS !)
23
IN PYTHON WE CAN USE
ABSTRACT BASE
CLASSES TO DEFINE
HOW CLASSES NEED TO
BEHAVE
(ENFORCED AT INSTANTIATION TIME)
24
from abc import ABCMeta, abstractmethod
class Singer(metaclass=ABCMeta):
@abstractmethod
def sing(self, tune):
pass
class GuitarPlayer(metaclass=ABCMeta):
@abstractmethod
def rock(self, tune):
pass
class Rocker(Singer, GuitarPlayer):
def sing(self, tune):
return f"YEAH {tune}"
def rock(self, tune):
return f"! {tune} !"
class BadRocker(Singer):
pass
25
>>> from abstract_example import Rocker
>>> gene_simmons = Rocker()
>>> gene_simmons.sing("Psycho Circus")
YEAH Psycho Circus
>>> gene_simmons.rock("Strutter")
! Strutter !
26
>>> from abstract import BadRocker
>>> foo = BadRocker()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class BadRocker
with abstract methods sing
27
WITH ABCS & MYPY WE
CAN ENSURE TYPE
SAFETY
28
def max_rock(rocker: Rocker, tune: str) -> str:
return f"{rocker.rock(rocker.sing(tune))}"
gene_simmons = Rocker()
max_rock(gene_simmons, "I was made for loving you")
This is all cool for mypy
29
justin_bieber = Singer()
max_rock(justin_bieber, "Some crap")
Mypy says:
abstract_example.py:34: error:
Argument 1 to "max_rock"
has incompatible type "Singer";
expected "Rocker"`
30
ALTHOUGH SCALA HAS
abstract (LIKE
JAVA), THE PREFERRED
WAY IS USING TRAITS
SCALA, ABSTRACT -> STAIRCASE, DUCHAMP
31
trait CanSing {
def sing(tune: String): String = {
tune
}
}
case class Singer(name: String, band: String) extends CanSing
trait PlaysGuitar {
def rock(tune: String): String
}
case class Rocker(name: String, band: String) extends CanSing with PlaysGuitar {
override def sing(tune: String): String = {
s"YEAH ${tune}"
}
def rock(tune: String): String = {
s"! ${tune} !"
}
}
32
object TraitsExamples {
def maxRock(rocker: Rocker, tune: String): String =
s"${rocker.rock(rocker.sing(tune))}"
def guitarIt(player: PlaysGuitar, tune: String): String =
s"${player.rock(tune)}"
def main(args: Array[String]): Unit = {
val geneSimmons = Rocker("Gene Simmons", "KISS")
println(maxRock(geneSimmons, "I was made for loving you"))
println(guitarIt(geneSimmons, "Strutter"))
// val justinBieber = Singer("Justin Bieber", "himself")
// maxRock(justinBieber, "some crap")
// traits.scala:24: error: type mismatch;
// found : Singer
// required: Rocker
}
}
33
!
IMPLICITS IN SCALA
!34
THEY DON'T MAKE MUCH
SENSE IN PYTHON3
BUT
IN SCALA THEY LET YOU
DO A BIT OF MAGIC
3
We have monkey-patching, but it breaks mypy type checking though
35
object Something {
private def doWhatever(config: Config) = {
implicit val spark = SparkSession.builder
.appName(Config.appName)
.getOrCreate()
Another().doCoolSparkyStuff(42)
}
}
class Another {
def doCoolSparkyStuff(num: Int)(implicit spark: SparkSession): Int = {
...
}
}
36
AT FIRST LOOK THEY LOOK LIKE
GLOBAL VARIABLES, BUT STRICT TYPE
CHECKING AND SCOPING MAKE
IMPLICITS TERRIBLY USEFUL
37
This slide intentionally left blank because type classes
are too weird
38
AFTER WORKING A BIT WITH SCALA
I HAVE DISCOVERED MANY, MANY
THINGS WE CAN DO IN PYTHON
EASIER AND BETTER
LEARN SOME SCALA AND COME
BACK TO PYTHON!
39
You can find code samples and the presentation source in
github.com/rberenguel/snakes_and_ladders
You can get the presentation from
Slideshare.com/rberenguel/SnakesAndLadders
40
REFERENCES AND SOURCES
SCALA GROWTH GRAPH
EPFL STAIRCASE BY MILES SABIN
SNAKE PICTURE
SNAKES AND LADDERS BOARD
TWELFTH DOCTOR GIF: FROM DOCTOR WHO
PILLARS
WITCH
NUDE DESCENDING A STAIRCASE, NO. 2 BY MARCEL DUCHAMP, WIKIPEDIA
FIXES IN PRODUCTION GIF: FROM BUSTER KEATON'S THE GENERAL
41

Más contenido relacionado

Similar a Snakes and ladders

Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
Kerry Buckley
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)
intelliyole
 

Similar a Snakes and ladders (20)

An Introduction to Scala (2014)
An Introduction to Scala (2014)An Introduction to Scala (2014)
An Introduction to Scala (2014)
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
 
Scala in a Java 8 World
Scala in a Java 8 WorldScala in a Java 8 World
Scala in a Java 8 World
 
7li7w devcon5
7li7w devcon57li7w devcon5
7li7w devcon5
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Kotlin: maybe it's the right time
Kotlin: maybe it's the right timeKotlin: maybe it's the right time
Kotlin: maybe it's the right time
 
Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)Feel of Kotlin (Berlin JUG 16 Apr 2015)
Feel of Kotlin (Berlin JUG 16 Apr 2015)
 
Macros and reflection in scala 2.10
Macros and reflection in scala 2.10Macros and reflection in scala 2.10
Macros and reflection in scala 2.10
 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
あたかも自然言語を書くようにコーディングしてみる
あたかも自然言語を書くようにコーディングしてみるあたかも自然言語を書くようにコーディングしてみる
あたかも自然言語を書くようにコーディングしてみる
 
Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
 
CoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseCoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming course
 
Scala vs Ruby
Scala vs RubyScala vs Ruby
Scala vs Ruby
 
Music as data
Music as dataMusic as data
Music as data
 

Último

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Último (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 

Snakes and ladders

  • 1. SNAKES & LADDERS: A TASTE OF SCALA FOR PYTHONISTAS 1
  • 2. SCALA(EPFL, LAUSANNE): FUNCTIONAL, OO, ADVANCED TYPE SYSTEM 2
  • 4. CAN WE GO FROM ONE TO THE OTHER? WHY SHOULD WE? 4
  • 5. SCALA IS THE LANGUAGE SHOWING FASTEST GROWTH IN THE DATA SCIENCE AND ENGINEERING FIELD1 1 The Most Popular Language For Machine Learning and Data Science Is … 5
  • 6. SOME REASONS WHY SCALA IS TRENDY 1. Runs in the JVM (interoperates with Java !) 2. Apache Spark is in Scala (can be used from Python ") 3. Scala developers earn more # 6
  • 7. BUT... 1. It runs on the JVM... Yikes! ! 2. Scala looks weird " 3. There is a lot of jargon in Scala # 7
  • 8. SO... TODAY WE'LL SEE HOW SCALA IS NOT THAT WEIRD IF YOU KNOW (WEIRD) PYTHON 8
  • 9. WHOAMI > Ruben Berenguel > PhD in Mathematics > (big) data consultant > Writing some Python for the past 12 years > Started with Scala a year ago > Right now at Affectv in London (we are hiring!) 9
  • 10. SCALA IS STATICALLY TYPED: NO int + str IN PRODUCTION 10
  • 11. WE HAVE MYPY: PYTHON'S STATIC TYPE CHECKER! 11
  • 12. # mypy_example.py: from typing import List answer = 42 # type: int dont = "panic" # type: str again = ["don't", "panic"] # type: List[str] Typecheck the file with: mypy mypy_example.py: ! 12
  • 13. # mypy_bad.py from typing import List answer = 42 # type: str dont = "panic" # type: int again = ["don't", "panic"] # type: List[float] Typecheck the file with: mypy mypy_example.py: ! mypy_bad.py:3: error: Incompatible types in assignment (expression has type "int", variable has type "str") mypy_bad.py:5: error: Incompatible types in assignment (expression has type "str", variable has type "int") mypy_bad.py:7: error: List item 0 has incompatible type "str" mypy_bad.py:7: error: List item 1 has incompatible type "str" 13
  • 14. SCALA LETS YOU DO FANCY TYPE-LEVEL STUFF, BUT THE BASICS ARE THE SAME AS IN MYPY14
  • 15. scala> val answer: Int = 42 answer: Int = 42 scala> val dont: String = "panic" dont: String = panic scala> val again: List[String] = List("don't", "panic") again: List[String] = List(don't, panic) scala> val noAnswer: Int = "panic" <console>:11: error: type mismatch; found : String("panic") required: Int val noAnswers: Int = "panic" 15
  • 16. CAN ALSO INFER TYPES scala> val answer = 42 answer: Int = 42 scala> val dont = "panic" dont: String = panic scala> val again = List("don't", "panic") again: List[String] = List(don't, panic) 16
  • 17. TYPE INFERENCE IS DONE BY THE TYPERtyper 4 THE MEAT AND POTATOES: TYPE THE TREES 17
  • 18. THE SCALA COMPILER HAS 24 PHASES (SLOOOW) 18
  • 20. scala> case class Person(name: String, age: Int) { | def greet = s"Hello, $name" | } defined class Person scala> val someone = Person("Joe Doe", 42) someone: Person = Person(Joe Doe,42) Q: is greet a function? A: In Scala, everything is (kind of) a function2 2 Everything is an expression (hence has a value), and that value is actually an object ! 20
  • 22. // someone: Person = Person(Joe Doe,42) scala> someone match { | case Person("Joe Doe", _) => someone.greet | case _ => "Who are you?" | } //res0: String = Hello, Joe Doe // someoneElse: Person = Person(Jane Doe,43) scala> someoneElse match { | case Person("Joe Doe", _) => someoneElse.greet | case _ => "Who are you?" | } //res1: String = Who are you? 22
  • 23. SADLY WE DON'T HAVE ANYTHING SIMILAR IN PYTHON(BUT WE LAUGH LAST: WHEN SCALA DEVS NEED TO MOCK OBJECTS !) 23
  • 24. IN PYTHON WE CAN USE ABSTRACT BASE CLASSES TO DEFINE HOW CLASSES NEED TO BEHAVE (ENFORCED AT INSTANTIATION TIME) 24
  • 25. from abc import ABCMeta, abstractmethod class Singer(metaclass=ABCMeta): @abstractmethod def sing(self, tune): pass class GuitarPlayer(metaclass=ABCMeta): @abstractmethod def rock(self, tune): pass class Rocker(Singer, GuitarPlayer): def sing(self, tune): return f"YEAH {tune}" def rock(self, tune): return f"! {tune} !" class BadRocker(Singer): pass 25
  • 26. >>> from abstract_example import Rocker >>> gene_simmons = Rocker() >>> gene_simmons.sing("Psycho Circus") YEAH Psycho Circus >>> gene_simmons.rock("Strutter") ! Strutter ! 26
  • 27. >>> from abstract import BadRocker >>> foo = BadRocker() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't instantiate abstract class BadRocker with abstract methods sing 27
  • 28. WITH ABCS & MYPY WE CAN ENSURE TYPE SAFETY 28
  • 29. def max_rock(rocker: Rocker, tune: str) -> str: return f"{rocker.rock(rocker.sing(tune))}" gene_simmons = Rocker() max_rock(gene_simmons, "I was made for loving you") This is all cool for mypy 29
  • 30. justin_bieber = Singer() max_rock(justin_bieber, "Some crap") Mypy says: abstract_example.py:34: error: Argument 1 to "max_rock" has incompatible type "Singer"; expected "Rocker"` 30
  • 31. ALTHOUGH SCALA HAS abstract (LIKE JAVA), THE PREFERRED WAY IS USING TRAITS SCALA, ABSTRACT -> STAIRCASE, DUCHAMP 31
  • 32. trait CanSing { def sing(tune: String): String = { tune } } case class Singer(name: String, band: String) extends CanSing trait PlaysGuitar { def rock(tune: String): String } case class Rocker(name: String, band: String) extends CanSing with PlaysGuitar { override def sing(tune: String): String = { s"YEAH ${tune}" } def rock(tune: String): String = { s"! ${tune} !" } } 32
  • 33. object TraitsExamples { def maxRock(rocker: Rocker, tune: String): String = s"${rocker.rock(rocker.sing(tune))}" def guitarIt(player: PlaysGuitar, tune: String): String = s"${player.rock(tune)}" def main(args: Array[String]): Unit = { val geneSimmons = Rocker("Gene Simmons", "KISS") println(maxRock(geneSimmons, "I was made for loving you")) println(guitarIt(geneSimmons, "Strutter")) // val justinBieber = Singer("Justin Bieber", "himself") // maxRock(justinBieber, "some crap") // traits.scala:24: error: type mismatch; // found : Singer // required: Rocker } } 33
  • 35. THEY DON'T MAKE MUCH SENSE IN PYTHON3 BUT IN SCALA THEY LET YOU DO A BIT OF MAGIC 3 We have monkey-patching, but it breaks mypy type checking though 35
  • 36. object Something { private def doWhatever(config: Config) = { implicit val spark = SparkSession.builder .appName(Config.appName) .getOrCreate() Another().doCoolSparkyStuff(42) } } class Another { def doCoolSparkyStuff(num: Int)(implicit spark: SparkSession): Int = { ... } } 36
  • 37. AT FIRST LOOK THEY LOOK LIKE GLOBAL VARIABLES, BUT STRICT TYPE CHECKING AND SCOPING MAKE IMPLICITS TERRIBLY USEFUL 37
  • 38. This slide intentionally left blank because type classes are too weird 38
  • 39. AFTER WORKING A BIT WITH SCALA I HAVE DISCOVERED MANY, MANY THINGS WE CAN DO IN PYTHON EASIER AND BETTER LEARN SOME SCALA AND COME BACK TO PYTHON! 39
  • 40. You can find code samples and the presentation source in github.com/rberenguel/snakes_and_ladders You can get the presentation from Slideshare.com/rberenguel/SnakesAndLadders 40
  • 41. REFERENCES AND SOURCES SCALA GROWTH GRAPH EPFL STAIRCASE BY MILES SABIN SNAKE PICTURE SNAKES AND LADDERS BOARD TWELFTH DOCTOR GIF: FROM DOCTOR WHO PILLARS WITCH NUDE DESCENDING A STAIRCASE, NO. 2 BY MARCEL DUCHAMP, WIKIPEDIA FIXES IN PRODUCTION GIF: FROM BUSTER KEATON'S THE GENERAL 41