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

Functionally Pragmatic - FP Days Cambridge 2012

Functionally Pragmatic - FP Days Cambridge 2012

Functional programming is on the rise, but it's not all just academic thought exercises, it can be a pragmatic solution to real world problems. I cover what I think pragmatism looks like and why functional languages like Scala can be pragmatic

Michael Brunton-Spall

October 25, 2012
Tweet

More Decks by Michael Brunton-Spall

Other Decks in Technology

Transcript

  1. Before I start ✤ Where I am coming from ✤

    Where we’ve been Saturday, 3 November 12
  2. Before I start ✤ Where I am coming from ✤

    Where we’ve been ✤ Where we are going Saturday, 3 November 12
  3. Important note ✤ Functionally inexperienced ✤ Monads, Applicative Functors, Partially

    Applied Functions? ✤ I’m pragmatic Saturday, 3 November 12
  4. History ✤ Java shop since 2006 ✤ guardian.co.uk: java +

    spring + velocity + hibernate + oracle Saturday, 3 November 12
  5. History ✤ Java shop since 2006 ✤ guardian.co.uk: java +

    spring + velocity + hibernate + oracle ✤ ~100k lines production java code (and ~35k xml...) Saturday, 3 November 12
  6. Content API ✤ Provide API to access all of our

    website content Saturday, 3 November 12
  7. Content API ✤ Provide API to access all of our

    website content ✤ Limited beta released early 2009 Saturday, 3 November 12
  8. Content API ✤ Provide API to access all of our

    website content ✤ Limited beta released early 2009 ✤ Started implementation of final version late 2009 Saturday, 3 November 12
  9. Content API ✤ Provide API to access all of our

    website content ✤ Limited beta released early 2009 ✤ Started implementation of final version late 2009 ✤ Live May 2010: http://content.guardianapis.com Saturday, 3 November 12
  10. November 2009 ✤ Started new Content API implementation ✤ java

    + guice + guice servlets + apache solr Saturday, 3 November 12
  11. January 2010 ✤ Bored! ✤ Tests in Scala with ScalaTest

    demo: mavan-scala-plugin, ScalaTest Saturday, 3 November 12
  12. January 2010 ✤ Bored! ✤ Tests in Scala with ScalaTest

    ✤ Maven = mixed scala-java with maven-scala-plugin demo: mavan-scala-plugin, ScalaTest Saturday, 3 November 12
  13. February 2010 ✤ Decided to convert to Scala ✤ IntelliJ

    did the work Saturday, 3 November 12
  14. February 2010 ✤ Decided to convert to Scala ✤ IntelliJ

    did the work ✤ scala + guice + guice servlets + apache solr Saturday, 3 November 12
  15. July 2010 ✤ Switch to simple-build-tool ✤ Mainly for incremental

    compilation ✤ ~ test-quick and ~ prepare-webapp also invaluable Saturday, 3 November 12
  16. Tony Hoare ✤ Comparing Engineers to Scientist ✤ Working together

    ✤ Let’s ask some basic questions Saturday, 3 November 12
  17. What timeframe are we talking about? ✤ Scientist - Long

    Term ✤ Engineer - Short Term Saturday, 3 November 12
  18. What timeframe are we talking about? ✤ Scientist - Long

    Term ✤ Engineer - Short Term ✤ Pragmatist - Today and tomorrow Saturday, 3 November 12
  19. How should we do this? ✤ Scientist - Idealist ✤

    Engineer - Compromise Saturday, 3 November 12
  20. How should we do this? ✤ Scientist - Idealist ✤

    Engineer - Compromise ✤ Pragmatist - Pragmatic Saturday, 3 November 12
  21. Getting results ✤ Scientist - Certainty ✤ Engineer - Risk

    Management ✤ Pragmatist - Suck it and see Saturday, 3 November 12
  22. When are we done? ✤ Scientist - Perfection ✤ Engineer

    - Adequacy ✤ Pragmatist - Minimum Viable Product Saturday, 3 November 12
  23. What are we trying to do? ✤ Scientist - Generality

    ✤ Engineer - Specificity Saturday, 3 November 12
  24. What are we trying to do? ✤ Scientist - Generality

    ✤ Engineer - Specificity ✤ Pragmatist - Particularity Saturday, 3 November 12
  25. How do we interact with wider systems? ✤ Scientist -

    Separation Saturday, 3 November 12
  26. How do we interact with wider systems? ✤ Scientist -

    Separation ✤ Engineer - Unification Saturday, 3 November 12
  27. How do we interact with wider systems? ✤ Scientist -

    Separation ✤ Engineer - Unification ✤ Pragmatist - Simplicity Saturday, 3 November 12
  28. How about ideals? ✤ Scientist - Unification ✤ Engineer -

    Diversity ✤ Pragmatist - Reusability Saturday, 3 November 12
  29. How do we value work? ✤ Scientist - Originality ✤

    Engineer - Best Practice Saturday, 3 November 12
  30. How do we value work? ✤ Scientist - Originality ✤

    Engineer - Best Practice ✤ Pragmatist - Individuality Saturday, 3 November 12
  31. Scientist view of functional ✤ Mathematical approach to development ✤

    Immutability and Category Theory allow proof of program Saturday, 3 November 12
  32. Scientist view of functional ✤ Mathematical approach to development ✤

    Immutability and Category Theory allow proof of program ✤ “I have only proved it correct, not tried it” Saturday, 3 November 12
  33. Scientist view of functional ✤ Mathematical approach to development ✤

    Immutability and Category Theory allow proof of program ✤ “I have only proved it correct, not tried it” ✤ The “inventors” of functional Saturday, 3 November 12
  34. Scientist view of functional ✤ Mathematical approach to development ✤

    Immutability and Category Theory allow proof of program ✤ “I have only proved it correct, not tried it” ✤ The “inventors” of functional ✤ Fierce protectors of it Saturday, 3 November 12
  35. Engineers view of functional ✤ My library can be type-safe

    so it can’t be misused Saturday, 3 November 12
  36. Engineers view of functional ✤ My library can be type-safe

    so it can’t be misused ✤ Inner details of my library don’t leak out because: Saturday, 3 November 12
  37. Engineers view of functional ✤ My library can be type-safe

    so it can’t be misused ✤ Inner details of my library don’t leak out because: ✤ Monads keep operations inside themselves Saturday, 3 November 12
  38. Engineers view of functional ✤ My library can be type-safe

    so it can’t be misused ✤ Inner details of my library don’t leak out because: ✤ Monads keep operations inside themselves ✤ Applicative Functors mean I can chain my operations nicely Saturday, 3 November 12
  39. Engineers view of functional ✤ My library can be type-safe

    so it can’t be misused ✤ Inner details of my library don’t leak out because: ✤ Monads keep operations inside themselves ✤ Applicative Functors mean I can chain my operations nicely ✤ Currying means I can return implementation easily. Saturday, 3 November 12
  40. Writing Minimal Code ✤ Not reductio ad absurdism ✤ DailyWTF

    ✤ Reducing to it’s minimal readable form Saturday, 3 November 12
  41. Writing Minimal Code ✤ Not reductio ad absurdism ✤ DailyWTF

    ✤ Reducing to it’s minimal readable form ✤ Not AbstractFactorySingletonBeanFactory Saturday, 3 November 12
  42. Frameworks ✤ Use a framework if it fits your problem

    ✤ Don’t use one Golden framework Saturday, 3 November 12
  43. Frameworks ✤ Use a framework if it fits your problem

    ✤ Don’t use one Golden framework ✤ Select a framework on ease of use and ease of hacking Saturday, 3 November 12
  44. Frameworks ✤ Use a framework if it fits your problem

    ✤ Don’t use one Golden framework ✤ Select a framework on ease of use and ease of hacking ✤ Know when to move away Saturday, 3 November 12
  45. Frameworks ✤ Use a framework if it fits your problem

    ✤ Don’t use one Golden framework ✤ Select a framework on ease of use and ease of hacking ✤ Know when to move away ✤ Django, Rails, Lift etc Saturday, 3 November 12
  46. Libraries ✤ If someone else has solved the problem, then

    use it ✤ Build a toolkit of libraries that just work for you Saturday, 3 November 12
  47. Libraries ✤ If someone else has solved the problem, then

    use it ✤ Build a toolkit of libraries that just work for you ✤ Simplicity over Correctness Saturday, 3 November 12
  48. Libraries ✤ If someone else has solved the problem, then

    use it ✤ Build a toolkit of libraries that just work for you ✤ Simplicity over Correctness ✤ e.g. scalaj-http vs dispatch - or sometimes you don’t care about async Saturday, 3 November 12
  49. Libraries ✤ If someone else has solved the problem, then

    use it ✤ Build a toolkit of libraries that just work for you ✤ Simplicity over Correctness ✤ e.g. scalaj-http vs dispatch - or sometimes you don’t care about async ✤ (and sometimes you do!) Saturday, 3 November 12
  50. Pragmatism Rules ✤ Move fast and break stuff ✤ Just

    Ship It ✤ Deployed code > perfect code Saturday, 3 November 12
  51. What it doesn’t mean ✤ I can use Type theory

    to ensure/prove I have fewer bugs Saturday, 3 November 12
  52. What it doesn’t mean ✤ I can use Type theory

    to ensure/prove I have fewer bugs ✤ Immutable monadic data structures == highly concurrent Saturday, 3 November 12
  53. What it doesn’t mean ✤ I can use Type theory

    to ensure/prove I have fewer bugs ✤ Immutable monadic data structures == highly concurrent ✤ Category theory means... Saturday, 3 November 12
  54. What it doesn’t mean ✤ I can use Type theory

    to ensure/prove I have fewer bugs ✤ Immutable monadic data structures == highly concurrent ✤ Category theory means... ✤ I have no idea what category theory means, but it’s awesome right? Saturday, 3 November 12
  55. Before that, why Scala? ✤ Scala as a gateway language

    ✤ “Java-without-the-semicolons” ® Saturday, 3 November 12
  56. Before that, why Scala? ✤ Scala as a gateway language

    ✤ “Java-without-the-semicolons” ® ✤ Easy to learn Saturday, 3 November 12
  57. Before that, why Scala? ✤ Scala as a gateway language

    ✤ “Java-without-the-semicolons” ® ✤ Easy to learn ✤ The C++ of functional Saturday, 3 November 12
  58. Before that, why Scala? ✤ Scala as a gateway language

    ✤ “Java-without-the-semicolons” ® ✤ Easy to learn ✤ The C++ of functional ✤ (Sorry Martin) Saturday, 3 November 12
  59. Team Building ✤ Encouraged people to learn something new ✤

    New ways of thinking Saturday, 3 November 12
  60. Team Building ✤ Encouraged people to learn something new ✤

    New ways of thinking ✤ Revisiting old university lessons Saturday, 3 November 12
  61. Team Building ✤ Encouraged people to learn something new ✤

    New ways of thinking ✤ Revisiting old university lessons ✤ Convinced some to stay Saturday, 3 November 12
  62. Team Recruiting ✤ The most common thing I hear in

    interviews Saturday, 3 November 12
  63. Team Recruiting ✤ The most common thing I hear in

    interviews ✤ “I want to learn Scala, I hear you are the best” Saturday, 3 November 12
  64. Team Recruiting ✤ The most common thing I hear in

    interviews ✤ “I want to learn Scala, I hear you are the best” ✤ Most common reason for joining Saturday, 3 November 12
  65. Team Recruiting ✤ The most common thing I hear in

    interviews ✤ “I want to learn Scala, I hear you are the best” ✤ Most common reason for joining ✤ Works as a great recruitment filter Saturday, 3 November 12
  66. Team Recruiting ✤ The most common thing I hear in

    interviews ✤ “I want to learn Scala, I hear you are the best” ✤ Most common reason for joining ✤ Works as a great recruitment filter ✤ Just say No to J2EE Saturday, 3 November 12
  67. Pragmatically Functional ✤ Let’s cover the 5 best bits of

    functional for a pragmatist Saturday, 3 November 12
  68. Pragmatically Functional ✤ Let’s cover the 5 best bits of

    functional for a pragmatist ✤ Readability Saturday, 3 November 12
  69. Pragmatically Functional ✤ Let’s cover the 5 best bits of

    functional for a pragmatist ✤ Readability ✤ Optionally defensive Saturday, 3 November 12
  70. Pragmatically Functional ✤ Let’s cover the 5 best bits of

    functional for a pragmatist ✤ Readability ✤ Optionally defensive ✤ Immutability Saturday, 3 November 12
  71. Pragmatically Functional ✤ Let’s cover the 5 best bits of

    functional for a pragmatist ✤ Readability ✤ Optionally defensive ✤ Immutability ✤ Collections Saturday, 3 November 12
  72. Pragmatically Functional ✤ Let’s cover the 5 best bits of

    functional for a pragmatist ✤ Readability ✤ Optionally defensive ✤ Immutability ✤ Collections ✤ Functional building blocks Saturday, 3 November 12
  73. Readability ✤ Fewer lines of code! ✤ Collections and Functional

    building blocks ✤ Released > perfect == lots of revisiting code Saturday, 3 November 12
  74. Readability ✤ Fewer lines of code! ✤ Collections and Functional

    building blocks ✤ Released > perfect == lots of revisiting code ✤ Less boiler plate Saturday, 3 November 12
  75. Readability ✤ Fewer lines of code! ✤ Collections and Functional

    building blocks ✤ Released > perfect == lots of revisiting code ✤ Less boiler plate ✤ Scala case classes Saturday, 3 November 12
  76. case class Question(pollId: Long, id: Long, count: Long) case class

    Answer(question: Question, id: Long, count: Long) vs public class DraftPollQuestion { ! protected Integer id; ! private List<DraftPollAnswer> answers = new ArrayList<DraftPollAnswer>(); ! public Integer getId() { return id; } ! public void setId(Integer id) { this.id = id; } ! public int hashCode() { return getId() == null ? 0 : getId() * LARGE_PRIME; } ! public boolean equals(Object that) { return EQUALITY.areEqual(this, that);} ! public String toString() { return getClass().getSimpleName() + "#" + (id == null ? "unsaved" : id); } ! public List<DraftPollAnswer> getAnswers() { return answers; } ! public void setAnswers(List<DraftPollAnswer> answers) { this.answers = answers; } ! public void addAnswer(DraftPollAnswer answer) { answers.add(answer); } ... } Saturday, 3 November 12
  77. Optionally defensive ✤ Null was the biggest mistake in computing.

    ✤ Null Pointer Exception is the bane of every java programmers life Saturday, 3 November 12
  78. Optionally defensive ✤ Null was the biggest mistake in computing.

    ✤ Null Pointer Exception is the bane of every java programmers life ✤ Don’t allow null values in your functional code, ever! Saturday, 3 November 12
  79. Optionally defensive ✤ Null was the biggest mistake in computing.

    ✤ Null Pointer Exception is the bane of every java programmers life ✤ Don’t allow null values in your functional code, ever! ✤ Where you may not have a value, use an option Saturday, 3 November 12
  80. Optionally defensive ✤ Null was the biggest mistake in computing.

    ✤ Null Pointer Exception is the bane of every java programmers life ✤ Don’t allow null values in your functional code, ever! ✤ Where you may not have a value, use an option ✤ Avoid “Optionitus” Saturday, 3 November 12
  81. // Add location filtering. 25 miles within your own location

    member.getAs[DBObject]("location") match { case Some(location) => { location.getAs[DBObject]("geolocation") match { case Some(geolocation : DBObject) => { geolocation.getAs[Double]("latitude") match { case Some(latitude : Double) => { geolocation.getAs[Double]("longitude") match { case Some(longitude : Double) => { SpatialFilterCriterion(latitude, longitude, 25, "miles") } case _ => } } case _ => } } case _ => } } case _ => } Saturday, 3 November 12
  82. // Add location filtering. 25 miles within your own location

    member.getAs[DBObject]("location") match { case Some(location) => { location.getAs[DBObject]("geolocation") match { case Some(geolocation : DBObject) => { geolocation.getAs[Double]("latitude") match { case Some(latitude : Double) => { geolocation.getAs[Double]("longitude") match { case Some(longitude : Double) => { SpatialFilterCriterion(latitude, longitude, 25, "miles") } case _ => } } case _ => } } case _ => } } case _ => } Phil Wills says “flatMap that shit” MBS says “for-comprehensions are awesome” Saturday, 3 November 12
  83. Immutability ✤ Immutability rocks ✤ Removes lots of bugs ✤

    Immutable by default... Saturday, 3 November 12
  84. Immutability ✤ Immutability rocks ✤ Removes lots of bugs ✤

    Immutable by default... ✤ Sometimes I really want a mutable field Saturday, 3 November 12
  85. object IndexDetails { private var _lastModified = (new DateTime).toUTCMidnight def

    lastModified = synchronized(_lastModified) def tickleLatestContent(lastModifiedTime: DateTime) { synchronized { if (_lastModified.isBefore(lastModifiedTime)) { _lastModified = lastModifiedTime } } } } val notifications = database { conn => conn.query(contentQuery, { row => IndexDetails.tickleLatestContent(content.lastLiveTime) // Generate an indexable bit of content } } Saturday, 3 November 12
  86. Collections ✤ A good collections framework is priceless ✤ Developers

    should know about Linked Lists, Red-Black trees etc, Saturday, 3 November 12
  87. Collections ✤ A good collections framework is priceless ✤ Developers

    should know about Linked Lists, Red-Black trees etc, ✤ But I don’t want to write one, I just want to get stuff done Saturday, 3 November 12
  88. Collections ✤ A good collections framework is priceless ✤ Developers

    should know about Linked Lists, Red-Black trees etc, ✤ But I don’t want to write one, I just want to get stuff done ✤ Academics have done this work for me. Saturday, 3 November 12
  89. Collections ✤ A good collections framework is priceless ✤ Developers

    should know about Linked Lists, Red-Black trees etc, ✤ But I don’t want to write one, I just want to get stuff done ✤ Academics have done this work for me. ✤ I’m really not going to do a better job Saturday, 3 November 12
  90. Functional Building Blocks ✤ Moving from “Java without semi-colons”® to

    actual functional ✤ Sequences of data structures Saturday, 3 November 12
  91. Functional Building Blocks ✤ Moving from “Java without semi-colons”® to

    actual functional ✤ Sequences of data structures ✤ Map, Filter, Accumulate Saturday, 3 November 12
  92. Functional Building Blocks ✤ Moving from “Java without semi-colons”® to

    actual functional ✤ Sequences of data structures ✤ Map, Filter, Accumulate ✤ Use where it makes sense to think of the problem as a pipeline of transformations Saturday, 3 November 12
  93. Functional on the rise ✤ Language doesn’t matter ✤ Scala

    ✤ Clojure ✤ Ruby Saturday, 3 November 12
  94. Functional on the rise ✤ Language doesn’t matter ✤ Scala

    ✤ Clojure ✤ Ruby ✤ Erlang Saturday, 3 November 12
  95. Functional on the rise ✤ Language doesn’t matter ✤ Scala

    ✤ Clojure ✤ Ruby ✤ Erlang ✤ Python Saturday, 3 November 12
  96. Functional on the rise ✤ Language doesn’t matter ✤ Scala

    ✤ Clojure ✤ Ruby ✤ Erlang ✤ Python ✤ F# Saturday, 3 November 12
  97. Scientists steer ✤ Engineers have taken years to catch up

    ✤ You’ll hate what we’re doing to your beautiful constructs Saturday, 3 November 12
  98. Scientists steer ✤ Engineers have taken years to catch up

    ✤ You’ll hate what we’re doing to your beautiful constructs ✤ We’ll learn Saturday, 3 November 12
  99. Scientists steer ✤ Engineers have taken years to catch up

    ✤ You’ll hate what we’re doing to your beautiful constructs ✤ We’ll learn ✤ Slowly Saturday, 3 November 12
  100. Pragmatists perform ✤ They’ll change over ✤ The day it’s

    faster in functional Saturday, 3 November 12
  101. Pragmatists perform ✤ They’ll change over ✤ The day it’s

    faster in functional ✤ The day it’s easier in functional Saturday, 3 November 12
  102. Pragmatists perform ✤ They’ll change over ✤ The day it’s

    faster in functional ✤ The day it’s easier in functional ✤ The day it’s the pragmatic solution Saturday, 3 November 12
  103. Pragmatists perform ✤ They’ll change over ✤ The day it’s

    faster in functional ✤ The day it’s easier in functional ✤ The day it’s the pragmatic solution ✤ They will never care for category theory Saturday, 3 November 12
  104. Pragmatists perform ✤ They’ll change over ✤ The day it’s

    faster in functional ✤ The day it’s easier in functional ✤ The day it’s the pragmatic solution ✤ They will never care for category theory ✤ probably Saturday, 3 November 12
  105. Conclusion ✤ Functional is here to stay ✤ until Prolog

    comes back anyway! :) Saturday, 3 November 12
  106. Conclusion ✤ Functional is here to stay ✤ until Prolog

    comes back anyway! :) ✤ Crossover languages help us migrate Saturday, 3 November 12
  107. Conclusion ✤ Functional is here to stay ✤ until Prolog

    comes back anyway! :) ✤ Crossover languages help us migrate ✤ “Purity” is of no pragmatic value Saturday, 3 November 12
  108. Conclusion ✤ Functional is here to stay ✤ until Prolog

    comes back anyway! :) ✤ Crossover languages help us migrate ✤ “Purity” is of no pragmatic value ✤ But it helps draft the map Saturday, 3 November 12