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

Realistic Functional Programming

Realistic Functional Programming

Slides from my keynote at Scala By The Schuylkill on January 24, 2017.

Michael Pilquist

January 24, 2017
Tweet

More Decks by Michael Pilquist

Other Decks in Technology

Transcript

  1. Realistic Functional Programming
    Michael Pilquist // @mpilquist
    Scala By The Schuylkill
    January 2017

    View Slide

  2. Who Am I?
    • Work at Comcast as part of CCAD & Video Org
    • Build state of the art control systems that manage set-top boxes, consumer
    devices, and video distribution equipment
    • Using Scala since 2008
    • Open source
    • Primary author of scodec, simulacrum, cedi-config
    • Maintainer of FS2 (formerly scalaz-stream)
    • Committer to Cats
    2

    View Slide

  3. View Slide

  4. Complexity is the root cause of the vast majority of problems
    with software today. Unreliability, late delivery, lack of security --
    often even poor performance in large-scale systems can all be
    seen as deriving ultimately from unmanageable complexity.
    — Ben Moseley & Peter Marks. Out of the Tar Pit. 2006

    View Slide

  5. …we have to keep it crisp,
    disentangled, and simple if we
    refuse to be crushed by the
    complexities of our own making…
    — Edgar Dijkstra. The Tide, Not
    the Waves. 1997
    Image credit: https://en.wikipedia.org/wiki/Edsger_W._Dijkstra#/media/File:Edsger_Wybe_Dijkstra.jpg CC BY-SA 3.0

    View Slide

  6. There are two ways of
    constructing a software design:
    One way is to make it so simple
    that there are obviously no
    deficiencies, and the other way
    is to make it so complicated that
    there are no obvious
    deficiencies. The first method is
    far more difficult.
    — C.A.R. Hoare. 1980 Turing
    Award Lecture, The Emperor’s
    Old Clothes.
    Image credit: https://en.wikipedia.org/wiki/Tony_Hoare#/media/File:Sir_Tony_Hoare_IMG_5125.jpg CC BY-SA 2.0 fr

    View Slide

  7. It demands the same skill,
    devotion, insight, and even
    inspiration as the discovery of
    the simple physical laws which
    underlie the complex
    phenomena of nature.
    — C.A.R. Hoare. 1980 Turing
    Award Lecture, The Emperor’s
    Old Clothes.
    Image credit: https://en.wikipedia.org/wiki/Tony_Hoare#/media/File:Sir_Tony_Hoare_IMG_5125.jpg CC BY-SA 2.0 fr

    View Slide

  8. Unfamiliar ≭ Complex

    View Slide

  9. View Slide

  10. …one of the great ideas in Functional
    Programming: constructing software
    systems from small expressions that
    can be understood in isolation.
    — José Manuel. http://jmct.cc/1/. 2017
    Image credit: https://pbs.twimg.com/profile_images/1295057087/25229_727959825617_10611734_41545628_160685_n_400x400.jpg
    Used with permission.

    View Slide

  11. Our ability to decompose a
    problem in to parts depends
    directly on our ability to glue
    solutions together.
    — John Hughes. Why Functional
    Programming Matters. 1990
    Image credit: https://en.wikipedia.org/wiki/John_Hughes_(computer_scientist)#/media/File:John_Hughes_(computer_scientist).jpg CC-BY-SA 3.0

    View Slide

  12. Abstraction
    Composition
    Precision
    — Rúnar Bjarnason.
    Constraints Liberate,
    Liberties Constrain. 2015
    Image credit: https://www.goodreads.com/photo/author/5774803.R_nar_Bjarnason/wiki/John_Hughes_(computer_scientist)#/media/File:John_Hughes_(computer_scientist).jpg
    Used with permission.

    View Slide

  13. def f(p: Boolean, q: Boolean): Boolean

    View Slide

  14. p q ⊤ p ⋁ q p ← q p p → q q p 㲗 q p ⋀ q
    t t t t t t t t t t
    t f t t t t f f f f
    f t t t f f t t f f
    f f t f t f t f t f
    p q ⊥ p ↓ q p ≰ q ¬p p ≯ q ¬q p ⊕ q p ↑ q
    t t f f f f f f f f
    t f f f f f t t t t
    f t f f t t f f t t
    f f f t f t f t f t

    View Slide

  15. def f[A](p: A, q: A): A

    View Slide

  16. def f[A](p: A, q: A): A = p
    def f[A](p: A, q: A): A = q

    View Slide

  17. Abstraction can appear to
    take you further and further
    away from reality, but really
    you’re getting closer and
    closer to the heart of the
    matter.

    — Eugenia Cheng, How To
    Bake π
    Image credit: Paul Crisanti of PhotoGetGo.
    Used with permission.

    View Slide

  18. trait Semigroup[A] {
    def combine(x: A, y: A): A
    }
    trait Monoid[A] extends Semigroup[A] {
    def id: A
    }

    View Slide

  19. val x = Map("odds"  Set(1, 3),
    "evens"  Set(2, 4))
    val y = Map("odds"  Set(5, 7),
    "evens"  Set(6, 8))
    Semigroup[Map[String, Set[Int]]].combine(x, y)
    // Map("odds"  Set(1, 3, 5, 7),
    "evens"  Set(2, 4, 6, 8))

    View Slide

  20. def sgMap[A, B]: Semigroup[Map[A, B]] =
    new Semigroup[Map[A, B]] {
    def combine(x: Map[A, B], y: Map[A, B]): Map[A, B] = {
    ???
    }
    }

    View Slide

  21. def sgMap[A, B]: Semigroup[Map[A, B]] =
    new Semigroup[Map[A, B]] {
    def combine(x: Map[A, B], y: Map[A, B]): Map[A, B] = {
    y.foldLeft(x) { case (acc, (a, b)) 
    val newB: B = acc.get(a).
    map(prevB  ???).
    getOrElse(b)
    acc.updated(a, newB)
    }
    }
    }

    View Slide

  22. def sgMap[A, B](
    implicit B: Semigroup[B]
    ): Semigroup[Map[A, B]] = new Semigroup[Map[A, B]] {
    def combine(x: Map[A, B], y: Map[A, B]): Map[A, B] = {
    y.foldLeft(x) { case (acc, (a, b)) 
    val newB: B = acc.get(a).
    map(prevB  B.combine(prevB, b)).
    getOrElse(b)
    acc.updated(a, newB)
    }
    }
    }

    View Slide

  23. Given a choice of solutions, pick the
    least powerful solution capable of
    solving your problem

    — Li Haoyi. Strategic Scala Style:
    Principle of Least Power. 2016
    Image credit: https://pbs.twimg.com/profile_images/475467756947513344/U2n2AzKv.jpeg
    Used with permission.

    View Slide

  24. View Slide

  25. Functional programming intellectual
    elitism is odd since a big point of FP
    is I’m not smart enough to program
    without discipline and tools.
    Read Backus: FP should aid in
    writing programs the author can
    understand (& prove things about).
    One discipline is simplicity!

    — Alex Gurney. 2016
    Image used with permission.

    View Slide

  26. Realistic Functional Programming
    • FP is a discipline of humility
    • Be intentional about adoption
    • Be disciplined about referential transparency, totality, & abstraction
    • Don't get overwhelmed by abstract nonsense
    26

    View Slide