Realistic Functional Programming
Michael Pilquist // @mpilquist
Scala By The Schuylkill
January 2017
Slide 2
Slide 2 text
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
Slide 3
Slide 3 text
No content
Slide 4
Slide 4 text
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
Slide 5
Slide 5 text
…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
Slide 6
Slide 6 text
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
Slide 7
Slide 7 text
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
Slide 8
Slide 8 text
Unfamiliar ≭ Complex
Slide 9
Slide 9 text
No content
Slide 10
Slide 10 text
…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.
Slide 11
Slide 11 text
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
Slide 12
Slide 12 text
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.
Slide 13
Slide 13 text
def f(p: Boolean, q: Boolean): Boolean
Slide 14
Slide 14 text
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
Slide 15
Slide 15 text
def f[A](p: A, q: A): A
Slide 16
Slide 16 text
def f[A](p: A, q: A): A = p
def f[A](p: A, q: A): A = q
Slide 17
Slide 17 text
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.
Slide 18
Slide 18 text
trait Semigroup[A] {
def combine(x: A, y: A): A
}
trait Monoid[A] extends Semigroup[A] {
def id: A
}
Slide 19
Slide 19 text
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))
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.
Slide 24
Slide 24 text
No content
Slide 25
Slide 25 text
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.
Slide 26
Slide 26 text
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