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@fosstodon.org North Bay Python 
 July 29th, 2023

Slide 2

Slide 2 text

LANGUAGES AND PROGRAMMING PARADIGMS 2 Functional Imperative (or is it procedural?) Object 
 Oriented

Slide 3

Slide 3 text

PARADIGMS Programming language categories 3

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

CALCULATOR PROGRAMMING WAS IMPERATIVE (1976) 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 fi t for Python: no tail-call optimisation

Slide 12

Slide 12 text

THE PROBLEM 
 WITH CATEGORIES Ontologies are so 1900’s 12

Slide 13

Slide 13 text

DEWEY DECIMAL CLASSIFICATION DDC 23 (2013 edition) 
 13

Slide 14

Slide 14 text

DEWEY'S CLASSIFICATION: A TIDY HIERARCHY 000 Computer science, information and general works 100 Philosophy and psychology 200 Religion 300 Social sciences 400 Language 500 Pure science 600 Technology 700 Arts and recreation 800 Literature 900 History and geography 14

Slide 15

Slide 15 text

A TIDY HIERARCHY 500 Natural sciences and mathematics 510 Mathematics 516 Geometry 516.3 Analytic geometries 516.37 Metric differential geometries 516.375 Finsler geometry 15

Slide 16

Slide 16 text

A TIDY HIERARCHY? 200 Religion 210 Philosophy and theory of religion 220 The Bible 230 Christianity 240 Christian practice and observance 250 Christian orders and local church 260 Social and ecclesiastical theology 270 History of Christianity 280 Christian denominations 290 Other religions 16

Slide 17

Slide 17 text

A TIDY HIERARCHY? 200 Religion ... 290 Other religions 291 No longer used — formerly "Comparative religion" 292 Classical religion (Greek and Roman religion) 293 Germanic religion 294 Religions of Indic origin 295 Zoroastrianism (Mazdaism, Parseeism) 296 Judaism 297 Islam, Bábism and Baháʼí Faith 298 No longer used — formerly "Mormonism" 299 Religions not provided for elsewhere 300 Social Sciences 17

Slide 18

Slide 18 text

CELESTIAL EMPORIUM By Jorge Luis Borges (1899-1986) 18

Slide 19

Slide 19 text

19

Slide 20

Slide 20 text

20

Slide 21

Slide 21 text

BETTER CATEGORIES? 21

Slide 22

Slide 22 text

A CLASSIFICATION BASED ON HARD FACTS 22

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

LANGUAGE CATEGORIES (1) 24

Slide 25

Slide 25 text

LANGUAGE CATEGORIES (2) 25

Slide 26

Slide 26 text

LANGUAGE CATEGORIES (3) 26

Slide 27

Slide 27 text

LANGUAGE CATEGORIES (4) 27

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

ONE CLASSIFICATION (ZOOM) 29

Slide 30

Slide 30 text

ONE CLASSIFICATION (ZOOM) 30 ???

Slide 31

Slide 31 text

“Ontology is overrated.” Clay Shirky 31

Slide 32

Slide 32 text

DESIGN PATTERNS When languages fall short 32

Slide 33

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

Slide 34

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

Slide 35

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

Slide 36

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

Slide 37

Slide 37 text

37

Slide 38

Slide 38 text

38

Slide 39

Slide 39 text

39

Slide 40

Slide 40 text

40

Slide 41

Slide 41 text

THE ITERATOR PATTERN The classic recipe 41

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 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) 44 for item in an_iterable: process(item) for item in an_iterable: process(item) •Terminates when a call to next() raises StopIteration.

Slide 45

Slide 45 text

ITERABLE VERSUS ITERATOR 45 • 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 46

Slide 46 text

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

Slide 47

Slide 47 text

CLASSIC ITERATOR IMPLEMENTATION The pattern as described by Gamma et. al. 47 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 48

Slide 48 text

BARBARA LISKOV'S CLU LANGUAGE 48 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 49

Slide 49 text

49

Slide 50

Slide 50 text

50

Slide 51

Slide 51 text

51

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

ITERABLE OBJECTS: THE KEY TO FOREACH • Python, Java & CLU let programmers de fi ne iterable objects 
 
 
 
 
 
 
 • Some languages don't o ff er this fl exibility • 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) 53 for item in an_iterable: process(item) for item in an_iterable: process(item)

Slide 54

Slide 54 text

ITERABLE TRAIN WITH A GENERATOR METHOD The Iterator pattern as a language feature: 54 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 55

Slide 55 text

COMPARE: CLASSIC ITERATOR × GENERATOR METHOD The classic Iterator recipe is obsolete in Python since v.2.2 (2001) 55 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 56

Slide 56 text

FEATURES Core features, not mere syntax 56

Slide 57

Slide 57 text

SAMPLE FEATURES ✖ LANGUAGES 57 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 58

Slide 58 text

SAMPLE FEATURES ✖ LANGUAGES 58 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 59

Slide 59 text

SAMPLE FEATURES ✖ LANGUAGES 59 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 60

Slide 60 text

SAMPLE FEATURES ✖ LANGUAGES 60 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 61

Slide 61 text

SAMPLE FEATURES ✖ LANGUAGES 61 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 62

Slide 62 text

SAMPLE FEATURES ✖ LANGUAGES 62 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 63

Slide 63 text

TYPE CHECKING & TYPE EXPRESSION 63

Slide 64

Slide 64 text

TYPE CHECKING & TYPE EXPRESSION IN PYTHON 64

Slide 65

Slide 65 text

TYPE CHECKING & TYPE EXPRESSION 65

Slide 66

Slide 66 text

STRATEGY IN PYTHON Leveraging Python’s fundamental features 66

Slide 67

Slide 67 text

CHOOSE AN ALGORITHM AT RUN TIME 67

Slide 68

Slide 68 text

68 Context Strategy Concrete strategies CHOOSE AN ALGORITHM AT RUN TIME

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

DOCTESTS FOR TWO ADDITIONAL CONCRETE STRATEGIES 70

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

STRATEGY ABC AND A CONCRETE STRATEGY 73

Slide 74

Slide 74 text

TWO CONCRETE STRATEGIES 74

Slide 75

Slide 75 text

FIRST-CLASS FUNCTION STRATEGY 75

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

CONTEXT: INVOKING THE STRATEGY FUNCTION 77

Slide 78

Slide 78 text

CONCRETE STRATEGIES AS FUNCTIONS 78

Slide 79

Slide 79 text

CONCRETE STRATEGIES AS FUNCTIONS (2) 79

Slide 80

Slide 80 text

VECTORIZING One aspect of functional programming 80

Slide 81

Slide 81 text

81

Slide 82

Slide 82 text

82

Slide 83

Slide 83 text

83

Slide 84

Slide 84 text

84

Slide 85

Slide 85 text

85

Slide 86

Slide 86 text

86

Slide 87

Slide 87 text

87

Slide 88

Slide 88 text

WRAPPING UP Learn the fundamentals 88

Slide 89

Slide 89 text

INSTEAD OF PARADIGMS… 89

Slide 90

Slide 90 text

FOCUS ON FUNDAMENTAL FEATURES 90

Slide 91

Slide 91 text

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

Slide 92

Slide 92 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 92 Inspired by Programming Language Pragmatics Michael L. Scott *

Slide 93

Slide 93 text

Luciano Ramalho lramalho@thoughtworks.com 
 Fediverse: @ramgarlic@fosstodon.org 
 PFKAT: @ramalhoorg Repo: github.com/ramalho/beyond-paradigms Slides: speakerdeck.com/ramalho THANKS !