Functional Programming in Java

2,279 views

Published on

Functional programming has started (re)gaining prominence in recent years, and with good reason too. Functional programs lend an elegant solution to the concurrency problem, result in more modular systems, are more concise and are easier to test. While modern languages like Scala and Clojure have embraced the functional style whole-heartedly, Java has lagged a bit behind in its treatment of functions as first-class citizens. With the advent of Java 8 and its support for lambdas, however, Java programmers can finally start reaping the power of functional programs as well. Even without Java 8, it is possible to adopt a functional style with the aid of excellent libraries such as Guava.

Published in: Technology, News & Politics
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,279
On SlideShare
0
From Embeds
0
Number of Embeds
297
Actions
Shares
0
Downloads
50
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Functional Programming in Java

  1. 1. Functional Programming in Java Prem Chandrasekaran & Jorge Lee ThoughtWorks Inc. Jan 29th 2014
  2. 2. What is Functional Programming?
  3. 3. An Inventory Management Example
  4. 4. Product
  5. 5. Item
  6. 6. Warehouse
  7. 7. Task 1 - Print names of all items in inventory
  8. 8. In Java 6…
  9. 9. Task 2 - Print names of all in-stock items of a category
  10. 10. Implementation
  11. 11. Now We Have…
  12. 12. Issues? • Too much duplication • Not sustainable • Too brittle
  13. 13. Issues? We need to externalize the search criteria
  14. 14. Refactor #1
  15. 15. Refactor #1
  16. 16. Refactor #1
  17. 17. Refactor #1
  18. 18. Refactor #2
  19. 19. Recap…
  20. 20. Now We Have…
  21. 21. Task 3 - Search for Products by name
  22. 22. Create Interface?
  23. 23. Now We Have…
  24. 24. Can we Refactor?
  25. 25. And Now…
  26. 26. Still Too Verbose?
  27. 27. IDE Magic!
  28. 28. That’s It! For Java 6
  29. 29. Enter Java 8
  30. 30. We Have…
  31. 31. Refactor #3 - Lambdas
  32. 32. Refactor #4 - No Types Necessary
  33. 33. Refactor #5 - Method References
  34. 34. Java 8 Gyaan • Functional Interface • Interface with a single method without an implementation • Can be enforced using the @FunctionalInterface annotation • There are several standard functional interfaces defined in the java.lang.function package
  35. 35. So…
  36. 36. Becomes…
  37. 37. Standard Functional Interfaces
  38. 38. Task 4 - Filter in-stock items and print the Item not just its name
  39. 39. We Have…
  40. 40. Becomes…
  41. 41. We Can Also…
  42. 42. Task 5 - Print the number of items in inventory
  43. 43. In Java 6…
  44. 44. What are we doing? 1. Providing an initial/default value 2. Accumulating a result 3. Combining the result
  45. 45. In Functional Terms…
  46. 46. Alternatively…
  47. 47. Alternatively…
  48. 48. Conveniently
  49. 49. Task 6 - Print number of items in all our warehouses
  50. 50. In Java 6…
  51. 51. In Functional Terms..
  52. 52. Also We Can…
  53. 53. Task 7 - Print the item with the least amount across warehouses
  54. 54. In Java 6
  55. 55. In Functional Terms…
  56. 56. Conveniently
  57. 57. Even more Conveniently
  58. 58. Even More Conveniently
  59. 59. Why Functional? • A newer different way of thinking • More modular • Side-effect free programs • Immutable programs • • Easier to write concurrent code Easier to test
  60. 60. What to do in Java 6? • Look in the Guava or Apache Commons Lang3 library • But… “As of Java 7, functional programming in Java can only be approximated through awkward and verbose use of anonymous classes. This is expected to change in Java 8, but Guava is currently aimed at users of Java 5 and above.! Excessive use of Guava's functional programming idioms can lead to verbose, confusing, unreadable, and inefficient code. These are by far the most easily (and most commonly) abused parts of Guava, and when you go to preposterous lengths to make your code "a one-liner," the Guava team weeps.” — Guava Documentation
  61. 61. What to do in Java 6 • First — Stop Whining • Prefer final variables • Minimize the use of void methods • Avoid use of mutable globals (statics) • Avoid returning nulls • Read Effective Java - And apply learnings!
  62. 62. Thank You!

×