Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beyond Paradigms — Berlin edition

Beyond Paradigms — Berlin edition

In the last 10 years a new approach to the study of programming languages has emerged: focus on features, not paradigms. This approach offers more direct, practical advice for programmers learning a new language, taking up coding idioms, and choosing suitable design patterns.

27c093d0834208f4712faaaec38c2c5c?s=128

Luciano Ramalho

October 10, 2019
Tweet

Transcript

  1. 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
  2. EMPORIO CELESTIAL By Jorge Luis Borges 2

  3. 3

  4. 4

  5. 5

  6. PARADIGMS Programming language categories 6

  7. CALCULATOR PROGRAMMING IS IMPERATIVE 7 HP-25 TI 58C

  8. HP-25 CALCULATOR PROGRAMMING LANGUAGE 8

  9. A SURVEY-STYLE PROGRAMMING LANGUAGES BOOK Programming Language Pragmatics,
 4th edition

    (2015) Michael L. Scott 9
  10. GCD ASM X86 Greatest
 common divisor in x86 Assembly (Scott,

    2015) 10
  11. GCD IN C, OCAML AND PROLOG 11

  12. GCD IN PYTHON Imperative style Functional style 12

  13. GCD IN PYTHON Imperative style Functional style 13 Bad fit

    for Python: no tail-call optimisation
  14. ONE CLASSIFICATION 14 Programming Language Pragmatics,
 4th edition (2015) Michael

    L. Scott
  15. ONE CLASSIFICATION (ZOOM) 15

  16. ONE CLASSIFICATION (ZOOM) 16 ???

  17. ANOTHER BOOK Princípios de Linguagens de Programação
 (2003) Ana Cristina

    Vieira de Melo Flávio Soares Corrêa da Silva 17
  18. 18

  19. 19 Lógicas

  20. THE LANGUAGE LIST 20

  21. LANGUAGES ARE MISSING… 21

  22. LANGUAGE CATEGORIES 22

  23. LANGUAGE CATEGORIES (2) 23

  24. LANGUAGE CATEGORIES (3) 24

  25. LANGUAGE CATEGORIES (4) 25

  26. CATEGORIES? Ontologies are so 1900’s 26

  27. A CLASSIFICATION BASED ON HARD FACTS 27

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

  29. “Ontology is overrated.” Clay Shirky 29

  30. A BETTER APPROACH Fundamental Features of Programming Languages 30

  31. TEACHING PROGRAMMING LANGUAGE THEORY 31

  32. A PAPER PRESENTING THE APPROACH 32

  33. A PAPER PRESENTING THE APPROACH 33

  34. A PAPER PRESENTING THE APPROACH 34

  35. A PAPER PRESENTING THE APPROACH 35

  36. THEORY IN PRACTICE WITH RACKET (A SCHEME DIALECT) 36

  37. THEORY IN PRACTICE WITH PASCAL (1990) 37

  38. FEATURES Core features, not mere syntax 38

  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. DESIGN PATTERNS When languages fall short 45

  46. 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
  47. 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
  48. 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
  49. 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
  50. 50

  51. 51

  52. 52

  53. 53

  54. THE ITERATOR PATTERN The classic recipe 54

  55. THE ITERATOR FROM THE GANG OF FOUR Design Patterns
 Gamma,

    Helm, Johnson & Vlissides
 ©1994 Addison-Wesley 55
  56. 56 Head First Design Patterns Poster
 O'Reilly
 ISBN 0-596-10214-3

  57. 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.
  58. 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!
  59. 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 >>>
  60. 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
  61. 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
  62. 62

  63. 63

  64. 64

  65. 65

  66. 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
  67. 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)
  68. 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
 <generator object __iter__ at 0x…>
 >>> next(it), next(it), next(it)
 ('car #1', 'car #2', 'car #3')
  69. 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
  70. STRATEGY IN PYTHON Leveraging Python’s fundamental features 70

  71. CHOOSE AN ALGORITHM AT RUN TIME 71

  72. 72 Context Strategy Concrete strategies CHOOSE AN ALGORITHM AT RUN

    TIME
  73. DOCTESTS FOR CONTEXT AND ONE CONCRETE STRATEGY 73 Instance of

    Strategy
 is given to Order constructor
  74. DOCTESTS FOR TWO ADDITIONAL CONCRETE STRATEGIES 74

  75. VARIATIONS OF STRATEGY IN PYTHON Classic implementation using ABC First-class

    function implementation Parameterised closure implementation Parameterised callable implementation 75
  76. CLASSIC STRATEGY: THE CONTEXT CLASS 76 Strategy is given to

    constructor Strategy is used here
  77. STRATEGY ABC AND A CONCRETE STRATEGY 77

  78. TWO CONCRETE STRATEGIES 78

  79. FIRST-CLASS FUNCTION STRATEGY 79

  80. CONTEXT: STRATEGY FUNCTION AS ARGUMENT 80 Strategy function is passed

    to Order constructor
  81. CONTEXT: INVOKING THE STRATEGY FUNCTION 81

  82. CONCRETE STRATEGIES AS FUNCTIONS 82

  83. CONCRETE STRATEGIES AS FUNCTIONS (2) 83

  84. PARAMETERISED STRATEGY
 WITH CLOSURE 84

  85. PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE 85 Promo is called with

    discount percent value
  86. PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE 86 Outer function gets percent

    argument Inner function carries percent binding
 in its closure
  87. LAMBDA: SHORTCUT TO DECLARE INNER FUNCTION 87

  88. PARAMETERISED STRATEGY
 WITH CALLABLE 88

  89. PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE 89 Promo is instantiated with

    discount percent value
  90. PROMOTION AS A CALLABLE CLASS 90

  91. CONCRETE STRATEGIES AS CALLABLE CONCRETE CLASSES 91

  92. WHICH IS MORE IDIOMATIC? Classes x functions 92

  93. WHICH IS MORE IDIOMATIC? Classic strategy feels too verbose for

    Python* 93 * Yes, this is subjective. I am talking about style!
  94. 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!
  95. WHICH IS MORE IDIOMATIC — WITH PARAMETER? Use of closure

    is common in Python •nonlocal was added in Python 3 to support it better 95
  96. 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
  97. WRAPPING UP Learn the fundamentals 97

  98. INSTEAD OF PARADIGMS… 98

  99. FOCUS ON FUNDAMENTAL FEATURES 99

  100. FEATURES ARE THE BEST GUIDE… 100 …to decide whether a

    particular pattern or implementation makes the most of the language.
  101. 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 *
  102. Luciano Ramalho luciano.ramalho@thoughtworks.com
 Twitter: @ramalhoorg / @standupdev Repo: github.com/standupdev/beyond-paradigms Slides:

    speakerdeck.com/ramalho VIELEN DANK !