functional programming in java - · pdf filefunctional programming in java ... • too...

62
Functional Programming in Java Prem Chandrasekaran & Jorge Lee ThoughtWorks Inc. Jan 29th 2014

Upload: buidat

Post on 12-Mar-2018

261 views

Category:

Documents


2 download

TRANSCRIPT

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!