Slide 1

Slide 1 text

E x a m p l e s i n P y t h o n a n d G o BEYOND PARADIGMS Understand language features,
 use the right patterns. Luciano Ramalho @ramalhoorg

Slide 2

Slide 2 text

2 Sometimes you need a blank template.

Slide 3

Slide 3 text

FLUENT PYTHON, MY FIRST BOOK Fluent Python (O’Reilly, 2015) Python Fluente (Novatec, 2015) Python к вершинам
 мастерства (DMK, 2015) 流暢的 Python (Gotop, 2016) also in Polish, Korean, etc… 3 4.7 stars at
 Amazon.com

Slide 4

Slide 4 text

PARADIGMS Programming language categories 4

Slide 5

Slide 5 text

CALCULATOR PROGRAMMING IS IMPERATIVE 5 HP-25 TI 58C

Slide 6

Slide 6 text

HP-25 CALCULATOR PROGRAMMING LANGUAGE 6

Slide 7

Slide 7 text

A SURVEY-STYLE PROGRAMMING LANGUAGES BOOK Programming Language Pragmatics,
 4th edition (2015) Michael L. Scott 7

Slide 8

Slide 8 text

GCD ASM X86 Greatest
 common divisor in x86 Assembly (Scott, 2015) 8

Slide 9

Slide 9 text

GCD IN C, OCAML AND PROLOG 9

Slide 10

Slide 10 text

GCD IN PYTHON Imperative style Functional style 10

Slide 11

Slide 11 text

GCD IN PYTHON Imperative style Functional style 11 Bad fit for Python: no tail-call optimisation

Slide 12

Slide 12 text

ONE CLASSIFICATION 12 Programming Language Pragmatics,
 4th edition (2015) Michael L. Scott

Slide 13

Slide 13 text

ONE CLASSIFICATION (ZOOM) 13

Slide 14

Slide 14 text

ONE CLASSIFICATION (ZOOM) 14 ???

Slide 15

Slide 15 text

ANOTHER BOOK Princípios de Linguagens de Programação
 (2003) Ana Cristina Vieira de Melo Flávio Soares Corrêa da Silva 15

Slide 16

Slide 16 text

16

Slide 17

Slide 17 text

17 Lógicas

Slide 18

Slide 18 text

CATEGORIES? Ontologies are so 1900’s 18

Slide 19

Slide 19 text

A CLASSIFICATION BASED ON HARD FACTS 19

Slide 20

Slide 20 text

A CLASSIFICATION BASED ON HARD FACTS? 20 “Noble” gases!?

Slide 21

Slide 21 text

“Ontology is overrated.” Clay Shirky 21

Slide 22

Slide 22 text

A BETTER APPROACH Fundamental Features of Programming Languages 22

Slide 23

Slide 23 text

TEACHING PROGRAMMING LANGUAGE THEORY 23

Slide 24

Slide 24 text

A PAPER PRESENTING THE APPROACH 24

Slide 25

Slide 25 text

A PAPER PRESENTING THE APPROACH 25

Slide 26

Slide 26 text

A PAPER PRESENTING THE APPROACH 26

Slide 27

Slide 27 text

A PAPER PRESENTING THE APPROACH 27

Slide 28

Slide 28 text

THEORY IN PRACTICE WITH RACKET (A SCHEME DIALECT) 28

Slide 29

Slide 29 text

DESIGN PATTERNS When languages fall short 29

Slide 30

Slide 30 text

GOF: CLASSIC BOOK BY THE “GANG OF FOUR” Design Patterns: Elements of Reusable Object-Oriented Software (1995) Erich Gamma
 Richard Helm
 Ralph Johnson
 John Vlissides 30

Slide 31

Slide 31 text

NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 31

Slide 32

Slide 32 text

NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 32

Slide 33

Slide 33 text

NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 33

Slide 34

Slide 34 text

34

Slide 35

Slide 35 text

35

Slide 36

Slide 36 text

36

Slide 37

Slide 37 text

FEATURES Core features, not mere syntax 37

Slide 38

Slide 38 text

SAMPLE FEATURES ✖ LANGUAGES 38 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 39

Slide 39 text

SAMPLE FEATURES ✖ LANGUAGES 39 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural Functions as objects Classes as objects

Slide 40

Slide 40 text

SAMPLE FEATURES ✖ LANGUAGES 40 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 41

Slide 41 text

SAMPLE FEATURES ✖ LANGUAGES 41 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 42

Slide 42 text

SAMPLE FEATURES ✖ LANGUAGES 42 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 43

Slide 43 text

SAMPLE FEATURES ✖ LANGUAGES 43 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 44

Slide 44 text

STRATEGY IN GO Leveraging Go’s fundamental features 44

Slide 45

Slide 45 text

CHOOSE AN ALGORITHM AT RUN TIME 45

Slide 46

Slide 46 text

46 CHOOSE AN ALGORITHM AT RUN TIME Context Strategy Concrete strategies

Slide 47

Slide 47 text

STRATEGY IN GO “Classic” implementation using an interface Implementation using first class functions 47

Slide 48

Slide 48 text

TESTS 48 interface/strategy_test.go Repo: https://github.com/ramalho/go-paradigma/

Slide 49

Slide 49 text

TESTS 49 interface/strategy_test.go

Slide 50

Slide 50 text

CUSTOMER AND LINE ITEMS 50 interface/store.go

Slide 51

Slide 51 text

CONTEXT YAND STRATEGY 51 interface/strategy.go

Slide 52

Slide 52 text

ONE CONCRETE STRATEGY 52 interface/store.go

Slide 53

Slide 53 text

MORE CONCRETE STRATEGIES 53 interface/store.go

Slide 54

Slide 54 text

STRATEGY WITH FIRST CLASS FUNCTIONS 54

Slide 55

Slide 55 text

TESTS WITH FUNCTIONS 55

Slide 56

Slide 56 text

CONCRETE STRATEGIES WITH FUNCTIONS 56

Slide 57

Slide 57 text

WHICH IS MORE IDIOMATIC? Interfaces x functions 57

Slide 58

Slide 58 text

INTERFACE X FIRST CLASS FUNCTIONS In the Go standard library, interfaces with a single method are common. 58

Slide 59

Slide 59 text

INTERFACE X FIRST CLASS FUNCTIONS But the http package supports both styles 59 *Kumar Iyer (ThoughtWorks): Higher-order functions vs interfaces in golang http://bit.ly/2j39uKh

Slide 60

Slide 60 text

STRATEGY IN PYTHON Leveraging Python’s fundamental features 60

Slide 61

Slide 61 text

CHOOSE AN ALGORITHM AT RUN TIME 61

Slide 62

Slide 62 text

62 Context Strategy Concrete strategies CHOOSE AN ALGORITHM AT RUN TIME

Slide 63

Slide 63 text

DOCTESTS FOR CONTEXT AND ONE CONCRETE STRATEGY 63 Instance of Strategy
 is given to Order constructor

Slide 64

Slide 64 text

DOCTESTS FOR TWO ADDITIONAL CONCRETE STRATEGIES 64

Slide 65

Slide 65 text

VARIATIONS OF STRATEGY IN PYTHON Classic implementation using ABC First-class function implementation Parameterised closure implementation Parameterised callable implementation 65

Slide 66

Slide 66 text

CLASSIC STRATEGY: THE CONTEXT CLASS 66 Strategy is given to constructor Strategy is used here

Slide 67

Slide 67 text

STRATEGY ABC AND A CONCRETE STRATEGY 67

Slide 68

Slide 68 text

TWO CONCRETE STRATEGIES 68

Slide 69

Slide 69 text

FIRST-CLASS FUNCTION STRATEGY 69

Slide 70

Slide 70 text

CONTEXT: STRATEGY FUNCTION AS ARGUMENT 70 Strategy function is passed to Order constructor

Slide 71

Slide 71 text

CONTEXT: INVOKING THE STRATEGY FUNCTION 71

Slide 72

Slide 72 text

CONCRETE STRATEGIES AS FUNCTIONS 72

Slide 73

Slide 73 text

CONCRETE STRATEGIES AS FUNCTIONS (2) 73

Slide 74

Slide 74 text

PARAMETERISED STRATEGY
 WITH CLOSURE 74

Slide 75

Slide 75 text

PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE 75 Promo is called with discount percent value

Slide 76

Slide 76 text

PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE 76 Outer function gets percent argument Inner function carries percent binding
 in its closure

Slide 77

Slide 77 text

LAMBDA: SHORTCUT TO DECLARE INNER FUNCTION 77

Slide 78

Slide 78 text

PARAMETERISED STRATEGY
 WITH CALLABLE 78

Slide 79

Slide 79 text

PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE 79 Promo is instantiated with discount percent value

Slide 80

Slide 80 text

PROMOTION AS A CALLABLE CLASS 80

Slide 81

Slide 81 text

CONCRETE STRATEGIES AS CALLABLE CONCRETE CLASSES 81

Slide 82

Slide 82 text

WHICH IS MORE IDIOMATIC? Classes x functions 82

Slide 83

Slide 83 text

WHICH IS MORE IDIOMATIC? Classic strategy feels too verbose for Python* First-class functions are very common in the standard library •The sorted built-in key argument is one example. 83 * Yes, this is subjective. I am talking about style!

Slide 84

Slide 84 text

WHICH IS MORE IDIOMATIC — WITH PARAMETER? Use of closure is common in Python •nonlocal was added in Python 3 to support it better Callable objects are uniquely Pythonic •Graham Dumpleton recommends callable classes as the best way to code decorators 84

Slide 85

Slide 85 text

WRAPPING UP Learn the fundamentals 85

Slide 86

Slide 86 text

INSTEAD OF PARADIGMS… 86 A B C D E F G

Slide 87

Slide 87 text

FOCUS ON FUNDAMENTAL FEATURES 87 A B C D E F G

Slide 88

Slide 88 text

…to decide whether a particular pattern or implementation makes sense. FEATURES ARE THE BEST GUIDE… A B C D E F G + ✔ ✔ ✔ ✔ + ✔ ✔ ✔ ✔ + ✔ ✔ ✔ ✔ ✔

Slide 89

Slide 89 text

WHY LEARN THE FUNDAMENTALS* Learn new languages faster Leverage language features Choose among alternative implementations Make good use of design patterns Emulate missing features when they are missing 89 Inspired by Programming Language Pragmatics Michael L. Scott *

Slide 90

Slide 90 text

Luciano Ramalho [email protected]
 Twitter: @standupdev Github: github.com/standupdev/paradigm-free Slides: speakerdeck.com/ramalho THANK YOU!

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content