Chapter 6. Where to Go From Here

Hopefully I’ve convinced you why functional programming is important for the challenges of our time. We only scratched the surface of this rich field. I hope you’ll continue learning and applying functional programming on your own.

So, where should you go next? I find it easier to learn abstract principles by writing real code. You could start by learning one of the scripting languages on the JVM, such as Groovy, JRuby, or Jython. While none of these languages is a functional language, per se, all have many functional features missing in Java, such as anonymous functions, collections with filter, map, fold, and other higher-order functions. (The names used by these languages may be different.) Along the way, you’ll find these languages useful for general development needs.

However, consider learning a real functional language, where you can see functional programming fully realized. In a few examples in this book we labored to represent some ideas in Java. Functional languages make them much easier to use.

Scala is my personal favorite, because it strives to unify both object-oriented and functional programming. Scala’s object-oriented support will let you continue to use familiar object-oriented concepts while you learn and start using functional concepts. Just be careful to avoid the trap of staying in familiar territory! [Wampler2011] provides a brief overview of the language and its compelling features. [Eckel2011] discusses how Scala has the succinct feel of a dynamically-typed language, like Python. For a more in-depth introduction, see Programming Scala [Wampler2009], the book I cowrote with Alex Payne. We tried hard to provide a pragmatic, developer-oriented introduction.

Clojure is the other well-known functional language on the JVM. It is a Lisp dialect that offers a powerful vision of how programming should be done, especially the management of state and mutability. In Clojure, all mutations of state are done through specific mechanisms, such as software transactional memory. Simple variable assignment is not supported. The greater discipline prevents many bugs and encourages you to think carefully about state and state transformations. Even if you don’t like Lisp syntax, it’s well worth learning Clojure, as the vision it presents is becoming a major influence on other languages. You can bet that whatever language you are using in 10 years will be heavily influenced by Clojure. Programming Clojure [Halloway2009] is an excellent introduction.

Finally, if you’re willing to go beyond the JVM, consider learning Haskell, which has been the incubator of many of the leading ideas in functional programming. Real World Haskell [O'Sullivan2009] and the whimsically named Learn You a Haskell for Great Good! [Lipovaca2011] are great introductions. Haskell is very different than most languages, so patience is required to learn it, but the profound insights it offers reward the effort.

If you are a Windows user, consider learning F#, Microsoft’s dialect of OCaml. F# is the first commercially-supported functional programming language available. OCaml itself has been used in projects on Wall Street, for example.

There are other great resources for further investigation, many of which are listed in the References. The videos on MSDN’s Channel 9, especially those by Erik Meijer, introduce basic and advanced functional topics [Channel 9]. The Structure and Interpretation of Computer Programs [Abelson1996] is a classic textbook for computer science. It’s not a book on functional programming, per se, but it walks the reader through a logical progression of computing principles, starting with functional programming concepts. Neal Ford’s “Functional Thinking” articles provide more examples of using functional concepts in several common languages [Ford2011]. Finally, Why Functional Programming Matters [Hughes1990] is a more advanced, yet approachable discussion on the benefits of functional programming.

Functional Tools for Java

There are also good options targeted at the Java programmer. The [Functional Java] APIs define anonymous function types, similar to those we defined in Chapter 2. You can also find various functional data structures, parser combinators, and an Actor library. Similarly, the [Totally Lazy] library offers lots of useful features.

The [Akka] Framework is a powerful, emerging suite of tools for building robust, concurrent applications. Akka includes one of the most performant and feature-complete Actor APIs available. Akka also integrates with many other third-party APIs to provide support for software transactional memory, web services, persistence stores, etc. Akka provides both Java and Scala versions of its APIs. I fully expect that Akka will become a widely used tool for JVM-based applications in the next several years, much as the Spring Framework became ubiquitous in the past decade [Spring].

A Recap

In the introduction, I discussed these factors that make me emphasize functional programming over object-oriented programming in my work.

I Have to Be Good at Writing Concurrent Programs

All of us must know how to write robust code that scales horizontally to multiple CPU cores and servers.

Most Programs Are Just Data Management Problems

Big data requires very efficient management of resources. Those efficiencies also benefit “small data” and “no data” projects. Overreliance on object-relational mapping and other forms of object middleware lead to code bloat, poor performance, and lower agility. We should remember, What’s the simplest thing that could possibly work? and stay focused on the minimal implementation required. We can express the problem domain through DSLs when appropriate, but we shouldn’t assume that our domain object models should be implemented in code.

Functional Programming Is More Modular

Functional programming moves the abstraction layers lower, to core data structures and combinator functions. Combined with immutable values and side-effect-free functions, the modularity and reusability of functional code is usually better than similar object-oriented code. Because objects are so free to expose abstractions any way they want, they are less reusable and composable, which is a paradox.

I Have to Work Faster and Faster

Functional programming keeps my code concise, by minimizing unnecessary and “one-off” implementation constructs, and it keeps my code logically correct. These qualities, in turn, keep me more agile over the life of the project as requirements change and features evolve.

Functional Programming Is a Return to Simplicity

Functional programming isn’t simple, but it represents a return to simplicity: the goal of minimizing implementation size and complexity by rethinking our ideas of appropriate design patterns and implementation idioms.

We learned several tools to improve modularity and reuse.

Custom classes aren’t always justified

If data fits in a collection, it probably shouldn’t have its own class.

Put your domain in domain-specific languages

Resist the temptation to faithfully capture your domain model in code. Instead, express your domain in domain-specific languages (DSLs), when useful, and use the most straightforward, concise implementation you can behind the DSL.

Function combinators

The combinators filter, map, and fold are flexible and composable tools because they are higher-order functions. We can exploit that in Java, too, if we standardize on generic Function types, rather than rely on one-off, special interface types for callbacks.

Use more generic types, like Function

Find ways to replace special purpose types with more general replacements. Really, just be more aggressive about applying the tools you already use to find abstractions that eliminate duplication in your code.

I hope you have found Functional Programming for Java Developers stimulating and informative. I hope you are motivated to learn and embrace this exciting trend in software development.

Exercises

  1. Look at the [Ninety Nine Problems], originally written for Prolog, and try working out the solutions in Java. It might be easier to use the ListModule we discussed in Chapter 3 or the [Functional Java] or [Totally Lazy] libraries. Note that you can find solutions for other languages, too.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset