Upload
premanand-chandrasekaran
View
1.004
Download
1
Embed Size (px)
DESCRIPTION
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.
Citation preview
Functional Programming in Java
Prem Chandrasekaran & Jorge Lee ThoughtWorks Inc.
Jan 29th 2014
What is Functional Programming?
An Inventory Management Example
Product
Item
Warehouse
Task 1 - Print names of all items in inventory
In Java 6…
Task 2 - Print names of all in-stock items of a
category
Implementation
Now We Have…
Issues?• Too much duplication
• Not sustainable
• Too brittle
Issues?
We need to externalize the search criteria
Refactor #1
Refactor #1
Refactor #1
Refactor #1
Refactor #2
Recap…
Now We Have…
Task 3 - Search for Products by name
Create Interface?
Now We Have…
Can we Refactor?
And Now…
Still Too Verbose?
IDE Magic!
That’s It! For Java 6
Enter Java 8
We Have…
Refactor #3 - Lambdas
Refactor #4 - No Types Necessary
Refactor #5 - Method References
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
So…
Becomes…
Standard Functional Interfaces
Task 4 - Filter in-stock items and print the Item -
not just its name
We Have…
Becomes…
We Can Also…
Task 5 - Print the number of items in inventory
In Java 6…
What are we doing?
1. Providing an initial/default value
2. Accumulating a result
3. Combining the result
In Functional Terms…
Alternatively…
Alternatively…
Conveniently
Task 6 - Print number of items in all our warehouses
In Java 6…
In Functional Terms..
Also We Can…
Task 7 - Print the item with the least amount across warehouses
In Java 6
In Functional Terms…
Conveniently
Even more Conveniently
Even More Conveniently
Why Functional?• A newer different way of thinking
• More modular
• Side-effect free programs
• Immutable programs
• Easier to write concurrent code
• Easier to test
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
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!
Thank You!