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

Beyond Paradigms

Beyond Paradigms

Understand language features,
 use the right patterns.

27c093d0834208f4712faaaec38c2c5c?s=128

Luciano Ramalho

February 17, 2018
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. 2 Sometimes you need a blank template.

  3. 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
  4. PARADIGMS Programming language categories 4

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

  6. HP-25 CALCULATOR PROGRAMMING LANGUAGE 6

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

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

    2015) 8
  9. GCD IN C, OCAML AND PROLOG 9

  10. GCD IN PYTHON Imperative style Functional style 10

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

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

    L. Scott
  13. ONE CLASSIFICATION (ZOOM) 13

  14. ONE CLASSIFICATION (ZOOM) 14 ???

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

    Vieira de Melo Flávio Soares Corrêa da Silva 15
  16. 16

  17. 17 Lógicas

  18. THE LANGUAGE LIST 18

  19. LANGUAGES ARE MISSING… 19

  20. LANGUAGE CATEGORIES 20

  21. LANGUAGE CATEGORIES (2) 21

  22. LANGUAGE CATEGORIES (3) 22

  23. LANGUAGE CATEGORIES (4) 23

  24. CATEGORIES? Ontologies are so 1900’s 24

  25. A CLASSIFICATION BASED ON HARD FACTS 25

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

  27. “Ontology is overrated.” Clay Shirky 27

  28. A BETTER APPROACH Fundamental Features of Programming Languages 28

  29. TEACHING PROGRAMMING LANGUAGE THEORY 29

  30. A PAPER PRESENTING THE APPROACH 30

  31. A PAPER PRESENTING THE APPROACH 31

  32. A PAPER PRESENTING THE APPROACH 32

  33. A PAPER PRESENTING THE APPROACH 33

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

  35. THEORY IN PRACTICE WITH PASCAL (1990) 35

  36. DESIGN PATTERNS When languages fall short 36

  37. 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 37
  38. FAULTS IN THE SPREAD OF PATTERNS 38

  39. 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. 39
  40. 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. 40
  41. 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. 41
  42. 42

  43. 43

  44. 44

  45. 45

  46. FEATURES Core features, not mere syntax 46

  47. SAMPLE FEATURES ✖ LANGUAGES 47 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
  48. SAMPLE FEATURES ✖ LANGUAGES 48 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
  49. SAMPLE FEATURES ✖ LANGUAGES 49 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
  50. SAMPLE FEATURES ✖ LANGUAGES 50 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
  51. SAMPLE FEATURES ✖ LANGUAGES 51 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
  52. SAMPLE FEATURES ✖ LANGUAGES 52 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
  53. STRATEGY IN PYTHON Leveraging Python’s fundamental features 53

  54. CHOOSE AN ALGORITHM AT RUN TIME 54

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

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

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

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

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

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

  61. TWO CONCRETE STRATEGIES 61

  62. FIRST-CLASS FUNCTION STRATEGY 62

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

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

  65. CONCRETE STRATEGIES AS FUNCTIONS 65

  66. CONCRETE STRATEGIES AS FUNCTIONS (2) 66

  67. PARAMETERISED STRATEGY
 WITH CLOSURE 67

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

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

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

  71. PARAMETERISED STRATEGY
 WITH CALLABLE 71

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

    discount percent value
  73. PROMOTION AS A CALLABLE CLASS 73

  74. CONCRETE STRATEGIES AS CALLABLE CONCRETE CLASSES 74

  75. WHICH IS MORE IDIOMATIC? Classes x functions 75

  76. 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. 76 * Yes, this is subjective. I am talking about style!
  77. 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 77
  78. WRAPPING UP Learn the fundamentals 78

  79. WHY LEARN THE FUNDAMENTALS* Learn new languages faster Leverage language

    features Choose among alternative implementations Make good use of design patterns Debug hard problems Emulate missing features when they are missing 79 Inspired by Programming Language Pragmatics Michael L. Scott *
  80. Luciano Ramalho luciano.ramalho@thoughtworks.com
 Twitter: @standupdev Github: github.com/standupdev/paradigm-free Slides: speakerdeck.com/ramalho ¡GRACIAS!