Slide 1

Slide 1 text

T h e o r y f o r p r a c t i c e BEYOND PARADIGMS Understand language features,
 use the right patterns. Luciano Ramalho @ramalhoorg

Slide 2

Slide 2 text

EMPORIO CELESTIAL By Jorge Luis Borges 2

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

PARADIGMS Programming language categories 6

Slide 7

Slide 7 text

CALCULATOR PROGRAMMING IS IMPERATIVE 7 HP-25 TI 58C

Slide 8

Slide 8 text

HP-25 CALCULATOR PROGRAMMING LANGUAGE 8

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

GCD IN C, OCAML AND PROLOG 11

Slide 12

Slide 12 text

GCD IN PYTHON Imperative style Functional style 12

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

ONE CLASSIFICATION (ZOOM) 15

Slide 16

Slide 16 text

ONE CLASSIFICATION (ZOOM) 16 ???

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

18

Slide 19

Slide 19 text

19 Lógicas

Slide 20

Slide 20 text

THE LANGUAGE LIST 20

Slide 21

Slide 21 text

LANGUAGES ARE MISSING… 21

Slide 22

Slide 22 text

LANGUAGE CATEGORIES 22

Slide 23

Slide 23 text

LANGUAGE CATEGORIES (2) 23

Slide 24

Slide 24 text

LANGUAGE CATEGORIES (3) 24

Slide 25

Slide 25 text

LANGUAGE CATEGORIES (4) 25

Slide 26

Slide 26 text

CATEGORIES? Ontologies are so 1900’s 26

Slide 27

Slide 27 text

A CLASSIFICATION BASED ON HARD FACTS 27

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

“Ontology is overrated.” Clay Shirky 29

Slide 30

Slide 30 text

A BETTER APPROACH Fundamental Features of Programming Languages 30

Slide 31

Slide 31 text

TEACHING PROGRAMMING LANGUAGE THEORY 31

Slide 32

Slide 32 text

A PAPER PRESENTING THE APPROACH 32

Slide 33

Slide 33 text

A PAPER PRESENTING THE APPROACH 33

Slide 34

Slide 34 text

A PAPER PRESENTING THE APPROACH 34

Slide 35

Slide 35 text

A PAPER PRESENTING THE APPROACH 35

Slide 36

Slide 36 text

THEORY IN PRACTICE WITH RACKET (A SCHEME DIALECT) 36

Slide 37

Slide 37 text

THEORY IN PRACTICE WITH PASCAL (1990) 37

Slide 38

Slide 38 text

FEATURES Core features, not mere syntax 38

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

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 Functions as objects Classes as objects

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

SAMPLE FEATURES ✖ LANGUAGES 44 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 45

Slide 45 text

DESIGN PATTERNS When languages fall short 45

Slide 46

Slide 46 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 46

Slide 47

Slide 47 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. 47

Slide 48

Slide 48 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. 48

Slide 49

Slide 49 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. 49

Slide 50

Slide 50 text

50

Slide 51

Slide 51 text

51

Slide 52

Slide 52 text

52

Slide 53

Slide 53 text

53

Slide 54

Slide 54 text

THE ITERATOR PATTERN The classic recipe 54

Slide 55

Slide 55 text

THE ITERATOR FROM THE GANG OF FOUR Design Patterns
 Gamma, Helm, Johnson & Vlissides
 ©1994 Addison-Wesley 55

Slide 56

Slide 56 text

56 Head First Design Patterns Poster
 O'Reilly
 ISBN 0-596-10214-3

Slide 57

Slide 57 text

THE FOR LOOP MACHINERY • In Python, the for loop, automatically: •Obtains an iterator from the iterable •Repeatedly invokes next() on the iterator, 
 retrieving one item at a time •Assigns the item to the loop variable(s) 57 for item in an_iterable: process(item) for item in an_iterable: process(item) •Terminates when a call to next() raises StopIteration.

Slide 58

Slide 58 text

ITERABLE VERSUS ITERATOR 58 • iterable: implements Iterable interface (__iter__ method) •__iter__ method returns an Iterator
 • iterator: implements Iterator interface (__next__ method) •__next__ method returns next item in series and •raises StopIteration to signal end of the series Python iterators are also iterable!

Slide 59

Slide 59 text

AN ITERABLE TRAIN An instance of Train can be iterated, car by car 59 >>> t = Train(3) >>> for car in t: ... print(car) car #1 car #2 car #3 >>>

Slide 60

Slide 60 text

CLASSIC ITERATOR IMPLEMENTATION The pattern as described by Gamma et. al. 60 class Train: def __init__(self, cars): self.cars = cars def __iter__(self): return TrainIterator(self.cars) class TrainIterator: def __init__(self, cars): self.next = 0 self.last = cars - 1 def __next__(self): if self.next <= self.last: self.next += 1 return 'car #%s' % (self.next) else: raise StopIteration() >>> t = Train(4) >>> for car in t: ... print(car) car #1 car #2 car #3 car #4

Slide 61

Slide 61 text

BARBARA LISKOV'S CLU LANGUAGE 61 CLU Reference Manual — B. Liskov et. al. — © 1981 Springer-Verlag — also available online from MIT: http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-225.pdf © 2010 Kenneth C. Zirkel — CC-BY-SA

Slide 62

Slide 62 text

62

Slide 63

Slide 63 text

63

Slide 64

Slide 64 text

64

Slide 65

Slide 65 text

65

Slide 66

Slide 66 text

ITERATION IN CLU CLU also introduced true iterators with yield and a generic for/ in statement. 66 year: 1975 CLU Reference Manual, p. 2 B. Liskov et. al. — © 1981 Springer-Verlag

Slide 67

Slide 67 text

ITERABLE OBJECTS: THE KEY TO FOREACH • Python, Java & CLU let programmers define iterable objects
 
 
 
 
 
 
 • Some languages don't offer this flexibility • C has no concept of iterables • In Go, only some built-in types are iterable and can be used with foreach (written as the for … range special form) 67 for item in an_iterable: process(item) for item in an_iterable: process(item)

Slide 68

Slide 68 text

ITERABLE TRAIN WITH A GENERATOR METHOD The Iterator pattern as a language feature: 68 class Train: def __init__(self, cars): self.cars = cars def __iter__(self): for i in range(self.cars): yield 'car #%s' % (i+1) Train is now iterable because __iter__ returns a generator! >>> t = Train(3) >>> it = iter(t) >>> it
 
 >>> next(it), next(it), next(it)
 ('car #1', 'car #2', 'car #3')

Slide 69

Slide 69 text

COMPARE: CLASSIC ITERATOR × GENERATOR METHOD The classic Iterator recipe is obsolete in Python since v.2.2 (2001) 69 class Train: def __init__(self, cars): self.cars = cars def __iter__(self): for i in range(self.cars): yield 'car #%s' % (i+1) class Train: def __init__(self, cars): self.cars = cars def __iter__(self): return IteratorTrem(self.cars) class TrainIterator: def __init__(self, cars): self.next = 0 self.last = cars - 1 def __next__(self): if self.next <= self.last: self.next += 1 return 'car #%s' % (self.next) else: raise StopIteration() Generator function handles the state of the iteration

Slide 70

Slide 70 text

STRATEGY IN PYTHON Leveraging Python’s fundamental features 70

Slide 71

Slide 71 text

CHOOSE AN ALGORITHM AT RUN TIME 71

Slide 72

Slide 72 text

72 Context Strategy Concrete strategies CHOOSE AN ALGORITHM AT RUN TIME

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

DOCTESTS FOR TWO ADDITIONAL CONCRETE STRATEGIES 74

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

STRATEGY ABC AND A CONCRETE STRATEGY 77

Slide 78

Slide 78 text

TWO CONCRETE STRATEGIES 78

Slide 79

Slide 79 text

FIRST-CLASS FUNCTION STRATEGY 79

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

CONTEXT: INVOKING THE STRATEGY FUNCTION 81

Slide 82

Slide 82 text

CONCRETE STRATEGIES AS FUNCTIONS 82

Slide 83

Slide 83 text

CONCRETE STRATEGIES AS FUNCTIONS (2) 83

Slide 84

Slide 84 text

PARAMETERISED STRATEGY
 WITH CLOSURE 84

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

LAMBDA: SHORTCUT TO DECLARE INNER FUNCTION 87

Slide 88

Slide 88 text

PARAMETERISED STRATEGY
 WITH CALLABLE 88

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

PROMOTION AS A CALLABLE CLASS 90

Slide 91

Slide 91 text

CONCRETE STRATEGIES AS CALLABLE CONCRETE CLASSES 91

Slide 92

Slide 92 text

WHICH IS MORE IDIOMATIC? Classes x functions 92

Slide 93

Slide 93 text

WHICH IS MORE IDIOMATIC? Classic strategy feels too verbose for Python* 93 * Yes, this is subjective. I am talking about style!

Slide 94

Slide 94 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. 94 * Yes, this is subjective. I am talking about style!

Slide 95

Slide 95 text

WHICH IS MORE IDIOMATIC — WITH PARAMETER? Use of closure is common in Python •nonlocal was added in Python 3 to support it better 95

Slide 96

Slide 96 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 96

Slide 97

Slide 97 text

WRAPPING UP Learn the fundamentals 97

Slide 98

Slide 98 text

INSTEAD OF PARADIGMS… 98

Slide 99

Slide 99 text

FOCUS ON FUNDAMENTAL FEATURES 99

Slide 100

Slide 100 text

FEATURES ARE THE BEST GUIDE… 100 …to decide whether a particular pattern or implementation makes the most of the language.

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

Luciano Ramalho [email protected]
 Twitter: @ramalhoorg / @standupdev Repo: github.com/standupdev/beyond-paradigms Slides: speakerdeck.com/ramalho VIELEN DANK !