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.

Luciano Ramalho

October 10, 2019
Tweet

More Decks by Luciano Ramalho

Other Decks in Programming

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. 3

  3. 4

  4. 5

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

    for Python: no tail-call optimisation
  6. ANOTHER BOOK Princípios de Linguagens de Programação
 (2003) Ana Cristina

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

  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 50

  19. 51

  20. 52

  21. 53

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

    Helm, Johnson & Vlissides
 ©1994 Addison-Wesley 55
  23. 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.
  24. 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!
  25. 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 >>>
  26. 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
  27. 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
  28. 62

  29. 63

  30. 64

  31. 65

  32. 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
  33. 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)
  34. 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')
  35. 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
  36. DOCTESTS FOR CONTEXT AND ONE CONCRETE STRATEGY 73 Instance of

    Strategy
 is given to Order constructor
  37. VARIATIONS OF STRATEGY IN PYTHON Classic implementation using ABC First-class

    function implementation Parameterised closure implementation Parameterised callable implementation 75
  38. PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE 86 Outer function gets percent

    argument Inner function carries percent binding
 in its closure
  39. WHICH IS MORE IDIOMATIC? Classic strategy feels too verbose for

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

    is common in Python •nonlocal was added in Python 3 to support it better 95
  42. 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
  43. FEATURES ARE THE BEST GUIDE… 100 …to decide whether a

    particular pattern or implementation makes the most of the language.
  44. 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 *