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.

Functional Programming, Is It Worth It?

4,119 views

Published on

From Shift Conference in Split, Croatia
June 2016

Published in: Software
  • Be the first to comment

Functional Programming, Is It Worth It?

  1. 1. Functional Programming, Is It Worth It? Andrew Rollins, Shift Conference, June 2016
  2. 2. Me: Andrew Rollins • http://www.andrewrollins.com • Twitter: @andrew311 • Boston, USA • Co-founder of Localytics • Long time functional programmer • Now at Sigma Prime Ventures (Boston VC) Andrew Rollins, Split, June 2016
  3. 3. Have you encountered… monad functor applicative and said “WTF?” Andrew Rollins, Split, June 2016
  4. 4. You are not alone. Andrew Rollins, Split, June 2016
  5. 5. Andrew Rollins, Split, June 2016 Learning FP is like Officer Barbrady learning to read (second season, fourth episode of South Park)
  6. 6. Andrew Rollins, Split, June 2016 Officer Barbrady confronts his illiteracy, learning to read so he can solve a crime!
  7. 7. Upon solving the crime, he’s given a copy of this: Andrew Rollins, Split, June 2016
  8. 8. Leading to a memorable South Park quote… Andrew Rollins, Split, June 2016
  9. 9. Andrew Rollins, Split, June 2016 Yes, at first I was happy to be learning how to read. It seemed exciting and magical, but then I read this: Atlas Shrugged by Ayn Rand. I read every last word of this garbage, and because of this piece of s**t, I am never reading again.
  10. 10. Andrew Rollins, Split, June 2016 Officer Barbrady’s experience sums up many people’s experience with Functional Programming.
  11. 11. Andrew Rollins, Split, June 2016 Exciting at first… But then you ecounter crazy s**t, and you have an Officer Barbrady moment.
  12. 12. Andrew Rollins, Split, June 2016 Crazy stuff like… •monads •functors •monoids •applicatives •category theory •blah blah blah
  13. 13. Andrew Rollins, Split, June 2016 I saw this happen across engineering at Localytics
  14. 14. Andrew Rollins, Split, June 2016 Does it have to be this way? Is FP worth it?
  15. 15. Andrew Rollins, Split, June 2016 I’m here to say… Yes, FP is worth it!
  16. 16. We can’t get hung up on this.
  17. 17. Andrew Rollins, Split, June 2016 Look past the… •monads •functors •monoids •applicatives •category theory •blah blah blah
  18. 18. And find the useful things… Andrew Rollins, Split, June 2016 •pure functions •higher order functions •immutable data •isolating side effects •using types effectively •and so on
  19. 19. Andrew Rollins, Split, June 2016 My goal for the rest of this presentation is to help you overcome your Officer Barbrady moment and find a path to success in FP.
  20. 20. Let’s get to it! Andrew Rollins, Split, June 2016
  21. 21. Tip FP is about a core set of principles Andrew Rollins, Split, June 2016
  22. 22. Principles •Pure functions •Higher order functions •Immutable data •Isolation of side effects Andrew Rollins, Split, June 2016
  23. 23. Pure functions •Purity is all about making functions self-contained and repeatable •foo(x) should NOT • read or manipulate state outside the function • change the caller’s version of x •foo(x) should • return the same results for the same argument, always Andrew Rollins, Split, June 2016
  24. 24. Why pure functions? •Because purity makes a function easier to reason about and test •Purity guarantees that you aren’t dependent on state changes outside of the function •Purity means that your tests can be concerned with simple input and output checks Andrew Rollins, Split, June 2016
  25. 25. Higher order functions •HoF is about being able to pass around functions as values Andrew Rollins, Split, June 2016
  26. 26. Why higher order functions? •Because they let us write useful methods like map •Most people use languages with HoF these days, so this shouldn’t require much convincing Andrew Rollins, Split, June 2016
  27. 27. Immutable data •Immutability is about locking down objects once they are created •An object should NOT update its internal state via object methods •State changes should be reflected in new objects •A programmer should not change variables once created Andrew Rollins, Split, June 2016
  28. 28. Why immutable data? •Because immutability makes it easier to reason about state in your program •Variable x is always the same x •Threads couldn’t possibly stomp on each others’ state if the state is immutable Andrew Rollins, Split, June 2016
  29. 29. Isolation of side effects •Isolation of side effects is about being disciplined with regards to side effects and where they happen •We recognize that code must have side effects to do anything useful, this is about techniques which let us minimize the surface area of side effects Andrew Rollins, Split, June 2016
  30. 30. Why isolation of side effects? •Because functions without side effects are easier to test and reason about •If we isolate side effects to a smaller portion of code, testing is easier Andrew Rollins, Split, June 2016
  31. 31. Read this for more about principles: “Intro To Functional Programming” by Marko Pavlovic http://markonis.github.io/functional/programming/2016/04/ 16/intro-to-functional-programming.html Andrew Rollins, Split, June 2016
  32. 32. Tip When learning FP, it’s important to separate the principles from the tools Andrew Rollins, Split, June 2016
  33. 33. Principle Measure twice, cut once Tool saw Andrew Rollins, Split, June 2016
  34. 34. Principle Don’t use null Tool Option Andrew Rollins, Split, June 2016
  35. 35. Tools • For comprehensions • Pattern matching • Functors • Monads • Monoids • Applicatives • Either • Option Andrew Rollins, Split, June 2016
  36. 36. Law of the Instrument "Give a small boy a hammer, and he will find that everything he encounters needs pounding.” - Abraham Kaplan, in 1964 Andrew Rollins, Split, June 2016
  37. 37. Law of the Instrument “If all you have is a hammer, everything looks like a nail.” - Abraham Maslow, in 1966 Andrew Rollins, Split, June 2016
  38. 38. Beware of this effect with… • Functors • Monads • Monoids • Applicatives • Either • Option Andrew Rollins, Split, June 2016
  39. 39. hammer = monad Andrew Rollins, Split, June 2016
  40. 40. Andrew’s Corollary to Law of the Instrument “If all you have is a hammer, and someone asks you to build a house, you’re gonna have a bad time.” - Andrew Rollins (me), in 2016 Andrew Rollins, Split, June 2016
  41. 41. It is exceptionally hard to build a house (application) when you only have a hammer (monad) Andrew Rollins, Split, June 2016
  42. 42. Treat FP tools like any other tools in your toolbox, and realize that it takes a set of tools to build a house. Andrew Rollins, Split, June 2016
  43. 43. Tip I like to think of functors, monads, etc. as a special kind of container. They are just codified patterns. Andrew Rollins, Split, June 2016
  44. 44. There are many kinds of containers: linked list, hash, array, tree, queue But they usually have common interfaces: Java Collection, Ruby Enumerable Andrew Rollins, Split, June 2016
  45. 45. Quoting Russ Bishop: Functors are containers you can call map on Monads are containers you can call flatMap on “Functors, Applicatives, and Monads in Plain English” http://www.russbishop.net/monoids-monads-and-functors Andrew Rollins, Split, June 2016
  46. 46. It’s a simplification, but it’ll get you started. Andrew Rollins, Split, June 2016
  47. 47. Also check out: “Functors, Applicatives, And Monads in Pictures” by Aditya Bhargava http://adit.io/posts/2013-04-17- functors,_applicatives,_and_monads_in_pictures.html Andrew Rollins, Split, June 2016
  48. 48. Tip Use libraries, but beware. Andrew Rollins, Split, June 2016
  49. 49. It helps to read about “Typeclasses”, especially for Haskell and Scala “Learn You a Haskell, Types and Typeclasses” http://learnyouahaskell.com/types-and-typeclasses Andrew Rollins, Split, June 2016
  50. 50. Many libraries use Typeclasses as an alternative to typical object inheritance and interfaces. It takes time to become comfortable with this pattern. Andrew Rollins, Split, June 2016
  51. 51. Wrapping up You already FP Andrew Rollins, Split, June 2016
  52. 52. Any time you… • Create a self-contained function without side effects • Pass around functions as input / output to other functions • Create immutable objects instead of mutating object state • Use return values instead of exceptions Andrew Rollins, Split, June 2016
  53. 53. Andrew Rollins, Split, June 2016 Then why does the Barbrady moment happen? Because FP wanders into some hard topics.
  54. 54. “A monad is just a monoid in the category of endofunctors, what's the problem?” - James Iry Andrew Rollins, Split, June 2016
  55. 55. Andrew Rollins, Split, June 2016 Don’t let it get you down. FP is just a style of programming. It might help you write better code. Don’t sweat the small stuff.
  56. 56. Takeaways •first focus on the principles •read those links •mess around with tools •don’t get bogged down by tools •try some popular libraries Andrew Rollins, Split, June 2016
  57. 57. Andrew Rollins, Split, June 2016 • Andrew Rollins • Twitter: @andrew311 • http://andrewrollins.com Happy programming! Thank you!

×