Scala is often touted as a tool for Functional Programming, but Functional Programming (FP) itself is left to differing and opinionated definitions by many of its practitioners. FP is a well defined approach to writing programs that we will uncover in this talk. It is a style that long predates Scala and many of the modern abstractions that are often referred to as "essential" tools.
We will walk through the origins of FP, providing historical context through Lisp and the research that brought us this paradigm. We'll define what FP is and what it is not, looking at how tools like purity and immutability enable the expressions at the heart of FP.
Finally we'll talk about practical approaches to Functional Programming in Scala, how you can and why you would use this style in your everyday work. This will _not_ be a talk about monads and type systems, but give you a pragmatic look at how to separate the syntactic sugar from the underlying principles.
10. How did we get here?
FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
Origins
11. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
“..in the same way that
music is not a set of
instruments, functional
programming is not a
set of abstractions that
we need to learn and
memorize.”
- José Calderón
Origins
15. The Lambda Calculus
Alonzo Church (1930s)
Theoretical foundation
Pure functions - no state
Not a programming language
@KELLEYROBINSONFUNCTIONAL PROGRAMMING ESSENTIALS
Origins
20. Logic Theory Machine
Newell and Simon (1956)
IPL (Information Processing
Language)
List processing
Recursion
FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
Origins
22. Functional
abstraction
Evolution of Functional Programming
An Early History
Higher level
languages
List processing &
recursion
Lisp popularizes
paradigm
FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
1930s 1950s
Origins
26. This was before…
Polymorphic Type Inference
Algebraic Data types
Lazy languages
Category-theoretic terminology
Introduction: Burge School of Functional Programming
FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
W.H. Burge (1975)
Essentials
40. Data Structures
Constructing and Deconstructing
1. How do you make the data?
2. How do you take it apart?
FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
Essentials
41. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
sealed abstract class List[+A]
case class Cons[A](head: A, tail: List[A]) extends List[A]
case object Null extends List[Nothing]
42. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
def listFunction(list: List[Int]) =
list match {
case Cons(x, xs) =>
// ... something that uses x and xs...
case Null =>
// ... something that can’t use x and xs...
}
43. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
def length(l: List[Int]): Int =
l match {
case Cons(x, xs) => 1 + length(xs)
case Null => 0
}
44. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
def sum(l: List[Int]): Int =
l match {
case Cons(x, xs) => x + sum(xs)
case Null => 0
}
45. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
def product(l: List[Int]): Int =
l match {
case Cons(x, xs) => x * product(xs)
case Null => 1
}
46. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
def list[A, B](f: (A,B) => B,
d: B,
xs: List[A]): B =
xs match {
case Cons(y, ys) => f(y, list(f,d,ys))
case Null => d
}
47. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
def list[A, B](f: (A,B) => B, d: B, xs: List[A]): B
def length ... = list((_, b) => 1 + b, 0, xs)
def sum ... = list((a, b) => a + b, 0, xs)
def product ... = list((a, b) => a * b, 1, xs)
54. “Jargon comes with an
inherent cost. It needs
to earn its place. (And it
often does: jargon is
necessary to discuss
complex and domain-
specific ideas.)”
- Bonnie Eisenman
FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON@KELLEYROBINSON
Why this matters
55.
56. “It is also a striking demonstration
of the fact that even definitions of
very high quality are often
inadequate as sources of
information on usage.”
60. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
“It may be fun to brag
about knowing how to
use "embedded DSL
with combinators,"
but is that really the
best thing to help your
startup succeed?”
- Hacker News
Why this matters
65. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
Storing data val var
Iterating map for loop
pattern
matching
procedural
statements
Deconstructing data
preferred possible
FP In Scala
66. What about…?
Cats…Scalaz…FREE MONADS!?
Basics are built into Scala
(think `map`, `fold`)
Libraries have other syntactic
sugar
FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
FP In Scala
67. • Have fun
• Promote understanding
• Be kind
@KELLEYROBINSONFUNCTIONAL PROGRAMMING ESSENTIALS
69. FUNCTIONAL PROGRAMMING ESSENTIALS
Special thanks to
• Sharethrough
• José Calderón
• Heather Miller
• Bonnie Eisenman
Resources
• Mentioned in this talk:
• Bonnie Eisenman on Scala Jargon
• José Calderón's Burge School of Functional Programming
• Conception, Evolution, and Application of Functional Programming Languages
• Original LambdaConf ladder and Updated LambdaConf ladder
• Vocabulary Processes
• Further Reading & Watching:
• Functional programming
• Lambda calculus
• John McCarthy - History of Lisp
• Lambda Calculus - Computerphile
• Mark Priestley - New Problems, New Paradigms
• John Hughes, Mary Sheeran - Keynote: Why Functional Programming Matters
• Vocabulary and Comprehension Research:
• The Relationship between Reading Comprehension and Vocabulary Knowledge
• The Relationship between Vocabulary Size and Reading Comprehension
• Vocabulary and Word Study to Increase Comprehension in Content Areas
• Why Vocabulary Instruction Matters
• Effects of long-term vocabulary instruction on reading comprehension
@KELLEYROBINSON
70. @KELLEYROBINSONFUNCTIONAL PROGRAMMING ESSENTIALSFUNCTIONAL PROGRAMMING ESSENTIALS
Photography credits
• Opening image
• Solving imaginary scaling issues
• Paradigms
• Orchestra
• Alonzo Church
• Philip Wadler
• Glitter
• Fortran
• Chess
• A Lisp machine in the MIT Museum
• 1975 SNL cast
• Side effects
• Escher print
• Legos
• Jargon
• Child reading
• University
• The thinker
• Jargon is alienating
• Cat typing
@KELLEYROBINSON