Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Forget what you think you know: Redefining functional programming for Scala

1,147 views

Published on

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.

Published in: Engineering
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Should you ever require the services of a hacker, i implore you to try your very best to hire only professionals. cyberhackpros@gmail.com will increase your chances of getting your job completed. i was able to hire the services of an elite, asides the fact that i was provided a permanent solution to the service he rendered me but he gave a very efficient customer experience. he carried me along with every process and didnt leave me in the dark.contact him via email/phone CYBERHACKPROS@GMAIL.COM or +1 916 378 4978 Tell him i reffered you.He will help you
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Forget what you think you know: Redefining functional programming for Scala

  1. 1. Forget what you think you know: Redefining Functional Programming Engineering Team Lead @kelleyrobinson Kelley Robinson
  2. 2. Forget what you think you know: Redefining Functional Programming
  3. 3. (This is not a real book)
  4. 4. “Standardized” Ladder of Functional Programming really FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
  5. 5. Forget what you think you know: Redefining Functional Programming Engineering Team Lead @kelleyrobinson Kelley Robinson
  6. 6. Engineering Team Lead @kelleyrobinson Kelley Robinson
  7. 7. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials Why this matters FP In Scala Origins
  8. 8. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials Why this matters FP In Scala Origins
  9. 9. Origins Paradigms FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
  10. 10. How did we get here? FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Origins
  11. 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
  12. 12. The Lambda Calculus Alonzo Church (1930s) @KELLEYROBINSONFUNCTIONAL PROGRAMMING ESSENTIALS Origins
  13. 13. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON λx.x+1 variable function application expression Origins
  14. 14. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON λx.x+1 scala> { x: Int => x + 1 } res1: Int => Int = <function1> Origins
  15. 15. The Lambda Calculus Alonzo Church (1930s) Theoretical foundation Pure functions - no state Not a programming language @KELLEYROBINSONFUNCTIONAL PROGRAMMING ESSENTIALS Origins
  16. 16. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
  17. 17. “Modern functional languages can be thought of as (nontrivial) embellishments of the lambda calculus” - Paul Hudak (1989)
  18. 18. Fortran John Backus (1954) FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Origins
  19. 19. Logic Theory Machine Newell and Simon (1956) FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Origins
  20. 20. Logic Theory Machine Newell and Simon (1956) IPL (Information Processing Language) List processing Recursion FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Origins
  21. 21. Lisp John McCarthy (1958) FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Origins
  22. 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
  23. 23. Paradigms Revisited FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Origins
  24. 24. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials Why this matters FP In Scala Origins
  25. 25. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials If you can understand expressions and data structures, you understand functional programming
  26. 26. Functional programming hasn’t materially changed since 1975 FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  27. 27. Recursive Programming Techniques W.H. Burge (1975) FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  28. 28. 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
  29. 29. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials Functional Programming doesn’t care about your monads or type systems
  30. 30. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Expressions Expressions Expressions Expressions Expressions Essentials
  31. 31. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON 3x2 + y3x2 + y Essentials
  32. 32. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON + (* (3, square(x)), y) 3x2 + y operators operands Essentials
  33. 33. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON + (* (3, square(2)), 1) for x = 2, y = 1 operators operands Essentials
  34. 34. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON + (* (3, 4), 1) operators operands for x = 2, y = 1 Essentials
  35. 35. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON + (12, 1) operators operands for x = 2, y = 1 Essentials
  36. 36. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON 13 for x = 2, y = 1 Essentials
  37. 37. Side Effects Changes outside of function scope Modifies state Not predictable FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  38. 38. Expressions Instead of statements Related: Pure functions Immutable data FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  39. 39. Referential transparency An expression can be replaced by its value without changing the program behavior FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  40. 40. Immutable data Avoid modifying state FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  41. 41. Stateful programming is inherently complex FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  42. 42. Expressions The programmer benefits Understanding Predictability Unit testing FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  43. 43. Data Structures Constructing and Deconstructing FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  44. 44. Data Structures Constructing and Deconstructing 1. How do you make the data? 2. How do you take it apart? FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials
  45. 45. 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]
  46. 46. 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... 
 }
  47. 47. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON def length(l: List[Int]): Int =
 l match {
 case Cons(x, xs) => 1 + length(xs)
 case Null => 0
 }
  48. 48. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON def sum(l: List[Int]): Int =
 l match {
 case Cons(x, xs) => x + sum(xs)
 case Null => 0
 }
  49. 49. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON def product(l: List[Int]): Int =
 l match {
 case Cons(x, xs) => x * product(xs)
 case Null => 1
 }
  50. 50. 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
 }
  51. 51. 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)
  52. 52. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials If you can understand expressions and data structures, you understand functional programming
  53. 53. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
  54. 54. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials Why this matters FP In Scala Origins
  55. 55. “Standardized” Ladder of Functional Programming really FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
  56. 56. “Modern” Functional Programming is cluttered with jargon FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Why this matters
  57. 57. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
  58. 58. “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
  59. 59. Research proves: Vocabulary and comprehension are directly correlated
  60. 60. “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.”
  61. 61. Vocabulary and context are necessary for understanding
  62. 62. Ladder of Functional Programming really “Standardized”**Updated!🙄** FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON
  63. 63. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON ✨ Expressions & Data Structures ✨ 🤔 Abstraction FooBar Buzzword Baz 🤷 trends core concepts} }
  64. 64. Someone has to maintain the code we write FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Why this matters
  65. 65. 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
  66. 66. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Jargon is alienating Why this matters
  67. 67. Please. Stop. Saying. This. Please. @KELLEYROBINSONFUNCTIONAL PROGRAMMING ESSENTIALS Why this matters (also not a real book)
  68. 68. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials Why this matters FP In Scala Origins
  69. 69. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON FP In Scala higher kinded types applied materialization existential types autotupling Dependent Types profunctor optics the cake pattern Existential quantifier CQRS Isomorphisms fundep materializers implicit resolution typeclass pattern vampire methods trampolining co-optic bananas the kitten pattern lupus ordering Comwell’s principle Meijer’s law Miller’s paradox Eta bindings endomonadic functors the jigsaw pattern Enum generators Dependent expressions Squid lenses hypelevel Scalareactive pandas multiverse inequalityproactive streams effectful constants lambda generators effectual effects semimonads WAR handles isomorphic esoterics Skolemization category philosophic Odersky indentation catamorphisms Hylomorphisms Lifted arrows recursive constants injuncted functions composable cats parachutes Mutumorphisms banana brackets algebraic idempotency contextual zygotes hyloadjoinct zygoid magma schema devolution Moors’ law compartial function generic serial versionable view triangular rings immuteratorreflexive ornaments Naperian functors delimited lambda buffer coinductive resumption monad lizards kleisli compositionCo-Yoneda lemmas GADT PJAs transfiguration covfefe monad
  70. 70. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON FP In Scala higher kinded types applied materialization existential types autotupling Dependent Types profunctor optics the cake pattern Existential quantifier CQRS Isomorphisms fundep materializers implicit resolution typeclass pattern vampire methods trampolining co-optic bananas the kitten pattern lupus ordering Comwell’s principle Meijer’s law Miller’s paradox Eta bindings endomonadic functors the jigsaw pattern Enum generators Dependent expressions Squid lenses hypelevel Scalareactive pandas multiverse inequalityproactive streams effectful constants lambda generators effectual effects semimonads WAR handles isomorphic esoterics Skolemization category philosophic Odersky indentation catamorphisms Hylomorphisms Lifted arrows recursive constants injuncted functions composable cats parachutes Mutumorphisms banana brackets algebraic idempotency contextual zygotes hyloadjoinct zygoid magma schema devolution Moors’ law compartial function generic serial versionable view triangular rings immuteratorreflexive ornaments Naperian functors delimited lambda buffer coinductive resumption monad lizards kleisli compositionCo-Yoneda lemmas GADT PJAs 🙅 transfiguration covfefe monad
  71. 71. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Storing data val var Iterating map for loop pattern matching procedural statements Deconstructing data preferred possible FP In Scala
  72. 72. 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
  73. 73. • Build community • Promote understanding • Minimize jargon @KELLEYROBINSONFUNCTIONAL PROGRAMMING ESSENTIALS
  74. 74. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON FP In Scala higher kinded types applied materialization existential types autotupling Dependent Types profunctor optics the cake pattern Existential quantifier CQRS Isomorphisms fundep materializers implicit resolution typeclass pattern vampire methods trampolining co-optic bananas the kitten pattern lupus ordering Comwell’s principle Meijer’s law Miller’s paradox Eta bindings endomonadic functors the jigsaw pattern Enum generators Dependent expressions Squid lenses hypelevel Scalareactive pandas multiverse inequalityproactive streams effectful constants lambda generators effectual effects semimonads WAR handles isomorphic esoterics Skolemization category philosophic Odersky indentation catamorphisms Hylomorphisms Lifted arrows recursive constants injuncted functions composable cats parachutes Mutumorphisms banana brackets algebraic idempotency contextual zygotes hyloadjoinct zygoid magma schema devolution Moors’ law compartial function generic serial versionable view triangular rings immuteratorreflexive ornaments Naperian functors delimited lambda buffer coinductive resumption monad lizards kleisli compositionCo-Yoneda lemmas GADT PJAs transfiguration covfefe monad
  75. 75. FUNCTIONAL PROGRAMMING ESSENTIALS @KELLEYROBINSON Essentials bitly.com/scalajargon
  76. 76. Thank You! @kelleyrobinson hello@krobinson.me
  77. 77. 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
  78. 78. @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

×