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

Beyond Paradigms (Go and Python examples)

Beyond Paradigms (Go and Python examples)

Understand language features,
 use the right patterns. Presented at ThoughtWorks Chicago.

Luciano Ramalho

June 19, 2018
Tweet

More Decks by Luciano Ramalho

Other Decks in Programming

Transcript

  1. E x a m p l e s i n

    P y t h o n a n d G o BEYOND PARADIGMS Understand language features,
 use the right patterns. Luciano Ramalho @ramalhoorg
  2. 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
  3. GCD IN PYTHON Imperative style Functional style 11 Bad fit

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

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

  6. 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 30
  7. 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. 31
  8. 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. 32
  9. 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. 33
  10. 34

  11. 35

  12. 36

  13. SAMPLE FEATURES ✖ LANGUAGES 38 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. 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 Functions as objects Classes as objects
  15. 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
  16. 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
  17. 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
  18. 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
  19. INTERFACE X FIRST CLASS FUNCTIONS In the Go standard library,

    interfaces with a single method are common. 58
  20. INTERFACE X FIRST CLASS FUNCTIONS But the http package supports

    both styles 59 *Kumar Iyer (ThoughtWorks): Higher-order functions vs interfaces in golang http://bit.ly/2j39uKh
  21. DOCTESTS FOR CONTEXT AND ONE CONCRETE STRATEGY 63 Instance of

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

    function implementation Parameterised closure implementation Parameterised callable implementation 65
  23. PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE 76 Outer function gets percent

    argument Inner function carries percent binding
 in its closure
  24. 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. 83 * Yes, this is subjective. I am talking about style!
  25. 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 84
  26. …to decide whether a particular pattern or implementation makes sense.

    FEATURES ARE THE BEST GUIDE… A B C D E F G + ✔ ✔ ✔ ✔ + ✔ ✔ ✔ ✔ + ✔ ✔ ✔ ✔ ✔
  27. WHY LEARN THE FUNDAMENTALS* Learn new languages faster Leverage language

    features Choose among alternative implementations Make good use of design patterns Emulate missing features when they are missing 89 Inspired by Programming Language Pragmatics Michael L. Scott *